selenium.common.exceptions.ElementClickInterceptedException:消息:元素点击被拦截:元素无法用Selenium和Python点击

VRX*_*VRX 12 python selenium xpath css-selectors webdriverwait

我目前正在从事一个自动填写表格的项目。填写表单时会出现下一个按钮,这就是为什么它给我一个错误。

我试过了:

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//input[@type='button' and @class='button']")))
Next = driver.find_element_by_xpath("//input[@type='button' and @class='button']")
Next.click()
Run Code Online (Sandbox Code Playgroud)

HTML:

<span class="btn">
    <input type="button" value="Next" class="button" payoneer="Button" data-controltovalidate="PersonalDetails" data-onfieldsvalidation="ToggleNextButton" data-onclick="UpdateServerWithCurrentSection();" id="PersonalDetailsButton">
     </input>
     <div class="clearfix"></div>
</span>
Run Code Online (Sandbox Code Playgroud)

错误:

selenium.common.exceptions.ElementClickInterceptedException:消息:元素点击被拦截:元素在点 (203, 530) 处不可点击。其他元素将收到点击:...(会话信息:chrome=76.0.3809.132)

小智 43

如果xpath的路径是对的,也许你可以试试这个方法来解决这个问题。将旧代码替换为以下代码:

button = driver.find_element_by_xpath("xpath")
driver.execute_script("arguments[0].click();", button)
Run Code Online (Sandbox Code Playgroud)

我之前解决了这个问题,但老实说,我不知道原因。


Deb*_*anB 13

\n\n

\n\n

\n\n

这个错误信息...

\n\n
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable at point (203, 530). Other element would receive the click: ... (Session info: chrome=76.0.3809.132)\n
Run Code Online (Sandbox Code Playgroud)\n\n

...意味着click()所需元素上的 被其他某个元素拦截,并且所需元素不可单击。

\n\n
\n\n

您需要考虑以下几点:

\n\n
    \n
  • 当使用Selenium进行自动化time.sleep(secs)时,在没有任何特定条件的情况下使用Selenium 来实现自动化会达不到 自动化的目的,应该不惜一切代价避免。根据文档:
  • \n
\n\n
\n

time.sleep(secs)将当前线程的执行暂停给定的秒数。该参数可以是浮点数以指示更精确的睡眠时间。实际的挂起时间可能小于请求的时间,因为任何捕获的信号都将在执行该信号\xe2\x80\x99s 捕获例程后终止 sleep() 。此外,由于系统中其他活动的调度,暂停时间可能比请求的时间长任意量。

\n
\n\n\n\n
\n\n

解决方案

\n\n

要单击值为“下一步”的按钮,您可以使用以下任一定位器策略

\n\n
    \n
  • 使用CSS_SELECTOR

    \n\n
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.button#PersonalDetailsButton[data-controltovalidate=\'PersonalDetails\']"))).click()\n
    Run Code Online (Sandbox Code Playgroud)
  • \n
  • 使用XPATH

    \n\n
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class=\'button\' and @id=\'PersonalDetailsButton\'][@data-controltovalidate=\'PersonalDetails\']"))).click()\n
    Run Code Online (Sandbox Code Playgroud)
  • \n
  • 注意:您必须添加以下导入:

    \n\n
    from selenium.webdriver.support.ui import WebDriverWait\nfrom selenium.webdriver.common.by import By\nfrom selenium.webdriver.support import expected_conditions as EC\n
    Run Code Online (Sandbox Code Playgroud)
  • \n
\n


小智 5

我遇到了类似的问题,.click() 总是返回不可点击的异常。这

driver.execute_script('arguments[0].click()', button)
Run Code Online (Sandbox Code Playgroud)

发挥魔力。您还可以使用它来执行任何其他 js 脚本

script = 'your JavaScript goes here'
element = driver.find_element_by_*('your element identifier goes here')
driver.execute_script(script, element)
Run Code Online (Sandbox Code Playgroud)