预期条件:等到一个元素可点击而其他元素消失
Expected condition: wait until one element clickable while other- dissapeared
我遇到以下问题:有一个页面(不幸的是它不是 public)带有 Register request
和 Approve request
按钮。单击按钮会打开一个带有弹出窗口的透明 div
。
算法如下:
1) 点击【=16=】;
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
P.S。我正在使用 time.sleep()
来执行此操作,但似乎是一种粗略的方法
已更新
我尝试使用 JavaScript
:
driver.execute_script('document.getElementById("ApproveButton").click();')
到目前为止一切正常...我想知道这是否真的是一种更好的点击按钮的方式,或者是否还会有一些障碍?
可以理解,使用 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 实例,而不是像您在示例中那样为每个等待创建一个新实例。无需一直创建新实例
我遇到以下问题:有一个页面(不幸的是它不是 public)带有 Register request
和 Approve request
按钮。单击按钮会打开一个带有弹出窗口的透明 div
。
算法如下:
1) 点击【=16=】;
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
P.S。我正在使用 time.sleep()
来执行此操作,但似乎是一种粗略的方法
已更新
我尝试使用 JavaScript
:
driver.execute_script('document.getElementById("ApproveButton").click();')
到目前为止一切正常...我想知道这是否真的是一种更好的点击按钮的方式,或者是否还会有一些障碍?
可以理解,使用 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 实例,而不是像您在示例中那样为每个等待创建一个新实例。无需一直创建新实例