【问题标题】:Python ElementClickInterceptedException in SeleniumSelenium 中的 Python ElementClickInterceptedException
【发布时间】:2020-07-11 19:47:06
【问题描述】:

我正在开发一个基于硒的 python 自动化项目。该代码通过连接到公司的内部网页 url 来自动执行一些常规任务。但是,代码偶尔会在两个按钮单击操作之间引发相同的异常。你能帮我弄清楚我错过了什么吗?提前致谢。 你可以在这里找到我的代码 sn-p 和错误截图:

selenium.common.exceptions.ElementClickInterceptedException:消息: 元素点击被拦截:元素......在点(180, 447)。其他元素会收到点击:...

pycharm_error_output_screenshot

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options

timeout = 200
options = Options()
options.headless = False
driver = webdriver.Chrome(options=options)
driver.implicitly_wait(3)
wait = WebDriverWait(driver, timeout)

driver.get("https://internal_web_appplication_page_for_company_x")

apply_button_v4 = wait.until(EC.visibility_of_element_located((By.XPATH, "//body//button[2]")))
apply_button_v4.click()
both_button = wait.until(EC.element_to_be_clickable((By.XPATH, "// label[3] // span[1]")))
wait.until_not(EC.element_to_be_clickable((By.CLASS_NAME, "map-mask")))
wait.until_not(EC.element_to_be_clickable((By.XPATH, "// *[ contains( @ class, 'map-mask')]")))
both_button.click()

截获的元素是全屏“加载”通知,在任何点击操作后短时间内可见。 此外,如果我在单击“both_button”之前放置 time.sleep(5),我的代码将按预期工作。

【问题讨论】:

    标签: python selenium pycharm webdriver selenium-chromedriver


    【解决方案1】:

    尝试以下解决方案来单击both_button。此异常有多种原因,例如 javascript 或 ajax 调用,元素不在视口中。

    actions = ActionChains(driver)
    actions.move_to_element(both_button)
    actions.click(both_button)
    actions.perform()
    

    【讨论】:

    • 刚刚尝试了解决方案,但运行代码后没有任何反应。代码 sn-p 运行时没有任何异常/错误,但未单击“both_button”。此外,如果我在单击“both_button”之前放置 time.sleep(5),我的原始代码将按预期工作。
    • 你能分享一下网址吗?我会从头开始检查。
    • 不幸的是,它是一个内部应用程序。这就是为什么您只能通过公司的 vpn 应用程序和凭据访问。
    • 感谢您的帮助。只是试图确定它是否有帮助。但不幸的是,问题仍然存在。实际上我没有遇到任何超时异常。这就是为什么即使我将超时增加设置为最大值也不起作用。问题是两个按钮单击操作之间的中间“加载”启动画面。 Selenium 在等待“加载”初始屏幕消失之前尝试单击。我的“等到”代码部分并没有真正等待它应该等待的时间。我不知道为什么。
    猜你喜欢
    • 2021-11-03
    • 2021-06-15
    • 2022-08-14
    • 1970-01-01
    • 2020-09-09
    • 2020-10-16
    • 1970-01-01
    • 2018-07-17
    相关资源
    最近更新 更多