首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WebDriverWait未按预期工作

WebDriverWait未按预期工作
EN

Stack Overflow用户
提问于 2018-04-11 12:43:16
回答 1查看 13.5K关注 0票数 15

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

有按钮在页面上,我点击说"custom_cols“。这个按钮为我打开一个窗口,在那里我可以选择我的列。

这个新窗口有时需要一些时间来打开(大约5秒)。为了处理这件事我用了

代码语言:javascript
复制
WebDriverWait 

延迟20秒。但是,有时它无法在新窗口中选择find元素,即使元素是可见的。在剩余的时间里,这种情况只发生一次-它正常工作。

我在其他地方也使用了相同的函数(WebDriverWait),这是预期的工作。我的意思是,它等待直到元素变得可见,然后点击它的时候,它找到它。

我的问题是,为什么新窗口上的元素是不可见的,即使我在等待元素变得可见。添加这里,我尝试增加延迟时间,但我仍然偶尔得到这个错误。

我的密码在这里

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-11 12:58:18

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

代码语言:javascript
复制
try:
    myElem = WebDriverWait(self.browser, delay).until(EC.element_to_be_clickable((By.XPATH , xpath)))

更新

根据您在评论中的反问,这里有三种方法的详细内容:

presence_of_element_located

定位(定位器)的定义如下:

代码语言:javascript
复制
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

定位(定位器)的定义如下:

代码语言:javascript
复制
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

可点击(定位器)的定义如下:

代码语言:javascript
复制
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. 
票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49775502

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档