Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 预期条件:等待一个元素可单击,而另一个元素消失_Python_Selenium_Selenium Webdriver_Selenium Chromedriver - Fatal编程技术网

Python 预期条件:等待一个元素可单击,而另一个元素消失

Python 预期条件:等待一个元素可单击,而另一个元素消失,python,selenium,selenium-webdriver,selenium-chromedriver,Python,Selenium,Selenium Webdriver,Selenium Chromedriver,我面临以下问题:有一个页面(不幸的是它不是公共页面)带有注册请求和批准请求按钮。单击按钮将打开一个带有弹出窗口的透明的div 算法如下: 1) 单击注册; 2) 在弹出的窗口中填写表格; 3) 点击Submit确认注册(关闭弹出窗口); 4) 单击Approve 因此,我使用以下代码: WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//button[@id="RegisterButton"]'))).

我面临以下问题:有一个页面(不幸的是它不是公共页面)带有
注册请求
批准请求
按钮。单击按钮将打开一个带有弹出窗口的透明的
div

算法如下: 1) 单击注册; 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\u to\u 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>

到目前为止,它是有效的。。。我想知道这是否真的是一种更好的点击按钮的方式,或者可能还有一些障碍?

可以理解,使用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的一个实例,而不是像您在示例中那样为每个等待创建一个新实例。不需要一直创建新实例。

可以理解,使用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的一个实例,而不是像您在示例中那样为每个等待创建一个新实例。无需一直创建新实例。

Hi@andersson您的代码工作正常,因为从错误中确认您得到的是“元素在点(338167)处不可单击”而且Thread.sleep也在工作,因为您应用EC的元素在DOM中存在,但它在DOM中没有固定的位置,而且这个错误通常发生在Google Chrome浏览器中。我敢打赌您在Chrome上。为了克服这个问题,请看一下这个链接是的,我使用
Chromedriver
。谢谢你的链接,我会检查的。是的,确保一个快速的解决方案是简单地更改浏览器,它肯定会工作thanks@RemcoW,超时增加无效hi@andersson您的代码工作正常,因为从错误中确认您得到的是“元素在点(338167)处不可单击”而且Thread.sleep也在工作,因为您应用EC的元素在DOM中存在,但它在DOM中没有固定的位置,而且这个错误通常发生在Google Chrome浏览器中。我敢打赌您在Chrome上。为了克服这个问题,请看一下这个链接是的,我使用
Chromedriver
。谢谢你的链接,我会检查的。是的,确保一个快速的解决方案是简单地更改浏览器,它肯定会工作thanks@RemcoW,超时增加不起作用