【问题标题】:Expected condition: wait until one element clickable while other- dissapeared预期条件:等到一个元素可点击而其他元素消失
【发布时间】:2016-05-27 09:09:22
【问题描述】:

我面临以下问题:有一个页面(不幸的是它不是公开的)带有一个 Register request 和一个 Approve request 按钮。单击一个按钮会打开一个透明的div 并弹出一个窗口。

算法如下: 1) 点击Register; 2)在弹出的弹出窗口中填写表格; 3)点击Submit确认注册(关闭弹窗); 4) 点击Approve

所以我使用以下代码:

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//button[@id="RegisterButton"]'))).click()
# ...Filling Register form...
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//button[@id="SubmitButton"]'))).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//button[@id="ApproveButton"]'))).click()

但尽管使用了EC.element_to_be_clickable(),但我收到以下错误:

 WebDriverException: Message: unknown error: 
 Element is not clickable at point (338, 167). 
 Other element would receive the click: <div class="modal fade" id="confirmWindow" style="display: block;">...</div>

似乎驱动程序认为Approve 已经可以点击并尝试在透明的div 仍然显示时点击它。

所以我需要一个Expected Conditions 语句来捕获div 已被选中时的状态,如果按钮可点击则Approve

附:我正在使用time.sleep() 来执行此操作,但似乎是一种粗略的方法

更新

我尝试使用JavaScript

driver.execute_script('document.getElementById("ApproveButton").click();')

到目前为止它有效......我想知道它是否真的是一个更好的点击按钮的方式,或者也可能存在一些障碍?

【问题讨论】:

  • 嗨@andersson,您的代码工作正常,因为您收到“元素在点 (338, 167) 处不可点击”的错误得到确认,而且 Thread.sleep 工作正常,因为您已为其应用 EC 的元素存在于 DOM 中,但它在 DOM 中没有固定位置,此错误通常发生在 Google Chrome 浏览器中,我敢打赌您使用的是 chrome
  • 要解决这个问题,请查看此链接stackoverflow.com/questions/37388866/…
  • 是的,我使用Chromedriver。谢谢你的链接,我去看看
  • 是的,一个快速的解决方案是简单地更改浏览器,它肯定会工作,谢谢
  • @RemcoW,超时增加无效

标签: python selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

可以理解的是,使用 time.sleep() 是您想要避免的。因为它点击了模态,我假设该按钮被认为是可点击的,即使模态还没有完全消失。在这种情况下,我会添加另一个等待,直到模态不再可见。

wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, '//button[@id="RegisterButton"]'))).click()
# ...Filling Register form...
wait.until(EC.element_to_be_clickable((By.XPATH, '//button[@id="SubmitButton"]'))).click()

# Wait till the modal is no longer visible
wait.until_not(EC.visibility_of_element_located((By.ID, 'confirmWindow')))

wait.until(EC.element_to_be_clickable((By.XPATH, '//button[@id="ApproveButton"]'))).click()

注意:我还创建了 WebDriverWait 的单个实例,而不是像您在示例中所做的那样为每个等待创建一个新实例。无需一直创建新实例。

【讨论】:

    猜你喜欢
    • 2019-11-25
    • 2018-11-23
    • 2016-06-08
    • 2016-12-19
    • 2018-03-16
    • 2018-10-26
    • 2022-01-22
    • 2017-04-16
    • 2015-01-12
    相关资源
    最近更新 更多