当前位置: 首页 > 面试题库 >

WebDriverWait无法按预期工作

籍利
2023-03-14
问题内容

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

我单击的页面上有一个按钮,说“ custom_cols”。此按钮为我打开一个窗口,从中可以选择列。

此新窗口有时需要一些时间才能打开(大约5秒钟)。所以我已经使用了

WebDriverWait

延迟为20秒。但是有时它无法在新窗口中选择查找元素,即使该元素可见。在其余时间中,这种情况仅发生十次一次。

我在其他地方也使用了相同的功能(WebDriverWait),并且可以正常使用。我的意思是,它等待直到元素变得可见,然后在找到它时单击它。

我的问题是,即使我正在等待元素可见,为什么新窗口上的元素也不可见。要在此处添加,我尝试增加延迟时间,但有时仍会收到该错误。

我的代码在这里

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()method时继续前进,则需要使用
element_to_be_clickable()

以下方法:

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

更新资料

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

present_of_element_located

present_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).

visible_of_element_located

visible_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.


 类似资料:
  • 我正在和selenium一起工作,刮一些数据。 有一个按钮在页面上,我正在点击说“Custom_Cols”。这个按钮为我打开了一个窗口,我可以在那里选择我的列。 我的问题是为什么新窗口上的元素不可见,即使我正在等待元素的可见。补充一下,我已经尝试增加延迟时间,但我还是会偶尔出现这个错误。 我的密码在这里

  • 问题内容: 经过测试后,我只能对已经解析过的JSON数据返回一个肯定值。 根据官方文件: isValidJSONObject返回一个布尔值,该布尔值指示是否可以将给定对象转换为JSON数据。 但是,尽管事实是我尝试将其从JSON转换为NSDictionary的对象都可以正常转换,但仍会返回。 这是我的代码: 我的日志包含以下内容: 然后是dict的输出,这是一个巨大的NSMutableDictio

  • 问题内容: 考虑以下可以在任何程序执行之前预加载的库: 问题是,尽管总是调用全局变量的构造函数,但对于某些程序却不调用析构函数,例如: 对于其他一些程序,按预期方式调用析构函数: 您能解释一下为什么在第一种情况下不调用析构函数吗?编辑:上面的问题已得到解答,即程序可能会使用_exit(),abort()退出。 然而: 有没有办法在预加载的程序退出时强制调用给定函数? 问题答案: 具有作为其初始化代

  • 我必须将日期-时间字符串转换为分区日期-时间对象。我使用DateTimeForman读取模式。根据留档,模式中的“Z”可以接受以下格式: /-0000 但是“分区约会”。parse(myDate,formatter)只适用于第一种情况;相反,在第二种情况下,代码生成一个异常。 我用的是8Java 我做错什么了?谢谢!

  • 我编写了自己的AtomicDouble类,还有一个BankAccount类,它执行两个简单的取款和存款操作,它有一个AtomicDouble实例(余额)。我的代码的问题是,当我在deposit()中调用addAndGet方法时,程序会陷入一个无限循环,compareAndSet()永远不会返回真值,但当我调试它时,currentValue和atomic中的值。get()相等,但此方法无法理解。 有

  • 问题内容: 我是python新手,我熟悉循环并尝试了一本书中的示例 但是输出如下 问题答案: 您必须改用(Python 2.x),因为它等效于,因此它会将输入解析并评估为有效的Python表达式。 注意: 不会捕获用户错误(例如,如果用户输入了一些无效的Python表达式)。可以这样做,因为它将输入转换为。有关更多信息,请阅读Python docs 。