Selenium中的Python ElementClickInterceptedException

问题描述 投票:0回答:1

我正在研究基于硒的python自动化项目。该代码通过连接到公司的内部网页网址来自动执行一些常规任务。但是,该代码有时会在两个按钮单击操作之间引发相同的异常。您能帮我弄清楚我想念的重点是什么吗?提前致谢。您可以在此处找到我的代码段和错误屏幕截图:

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个回答
0
投票

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

actions = ActionChains(driver)
actions.move_to_element(both_button)
actions.click(both_button)
actions.perform()
© www.soinside.com 2019 - 2024. All rights reserved.