WebDriverWait 未按预期工作 [英] WebDriverWait not working as expected

查看:30
本文介绍了WebDriverWait 未按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 selenium 来抓取一些数据.

I am working with selenium to scrape some data.

我点击的页面上有一个按钮说custom_cols".此按钮为我打开一个窗口,我可以在其中选择我的列.

There is button on the page that I am clicking say "custom_cols". This button opens up a window for me where I can select my columns.

这个新窗口有时需要一些时间才能打开(大约 5 秒).所以为了处理这个我使用了

This new window sometimes takes some time to open (around 5 seconds). So to handle this I have used

WebDriverWait 

延迟为 20 秒.但有时它无法在新窗口中选择查找元素,即使该元素可见.这种情况只有十次发生一次,其余时间它都可以正常工作.

with delay as 20 seconds. But some times it fails to select find elements on new window, even if the element is visible. This happens only once in ten times for rest of time it works properly.

我也在其他地方使用了相同的功能(WebDriverWait),它按预期工作.我的意思是它会等到元素可见,然后在找到它的那一刻点击它.

I have used same function(WebDriverWait) on other places also and it is works as expected. I mean it waits till the elements gets visible and then clicks it at the moment it finds it.

我的问题是为什么即使我正在等待元素可见,新窗口上的元素也不可见.在这里补充一下,我试图增加延迟时间,但仍然偶尔会出现该错误.

My question is why elements on new window is not visible even though I am waiting for element to get visible. To add here I have tried to increase delay time but still I get that error once in a while.

我的代码在这里

def wait_for_elem_xpath(self, delay = None, xpath = ""):
    if delay is None:
        delay = self.delay

    try:
        myElem = WebDriverWait(self.browser, delay).until(EC.presence_of_element_located((By.XPATH , xpath)))
    except TimeoutException:
        print ("xpath: Loading took too much time!")
    return myElem
select_all_performance = '//*[@id="mks"]/body/div[7]/div[2]/div/div/div/div/div[2]/div/div[2]/div[2]/div/div[1]/div[1]/section/header/div'
self.wait_for_elem_xpath(xpath = select_all_performance).click()

推荐答案

一旦您等待元素并在尝试调用 click() 方法而不是使用 presence_of_element_located 时向前移动() 方法需要使用 element_to_be_clickable() 如下:

Once you wait for the element and moving forward as you are trying to invoke click() method instead of using presence_of_element_located() method you need to use element_to_be_clickable() as follows :

try:
    myElem = WebDriverWait(self.browser, delay).until(EC.element_to_be_clickable((By.XPATH , xpath)))

<小时>

更新

根据您在评论中的反问,这里是三种方法的详细信息:


Update

As per your counter question in the comments here are the details of the three methods :

presence_of_element_located(locator) 定义如下:

class selenium.webdriver.support.expected_conditions.presence_of_element_located(locator)

Parameter : locator - used to find the element returns the WebElement once it is located

Description : An expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible or interactable (i.e. clickable). 

visibility_of_element_located

visibility_of_element_located(locator) 定义如下:

class selenium.webdriver.support.expected_conditions.visibility_of_element_located(locator)

Parameter : locator -  used to find the element returns the WebElement once it is located and visible

Description : An expectation for checking that an element is present on the DOM of a page and visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.

element_to_be_clickable

element_to_be_clickable(locator) 定义如下:

class selenium.webdriver.support.expected_conditions.element_to_be_clickable(locator)

Parameter : locator - used to find the element returns the WebElement once it is visible, enabled and interactable (i.e. clickable).

Description : An Expectation for checking an element is visible, enabled and interactable such that you can click it. 

这篇关于WebDriverWait 未按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆