ElementClickInterceptedException:消息:元素点击被拦截:元素 <label> 不能用 Selenium 和 Python 点击

Wil*_*am 7 python selenium xpath css-selectors webdriverwait

我试图单击“所有主题”和“所有状态”复选框,然后搜索结果。当我运行脚本时,会打开一个 1036x674 大小的 chrome 窗口。

如果我不理会窗口,我会收到元素点击拦截错误。如果我最小化或最大化窗口,我的脚本工作正常。

我正在使用 Selenium 3.141.0、chrome 76、chromedriver 76 和 python 3.6

chromedriver_path = r"C:\Users\path\to\chromedriver.exe"
browser = webdriver.Chrome(executable_path=chromedriver_path)
url = "http://www.ncsl.org/research/transportation/autonomous-vehicles-legislative-database.aspx"

topics_xpath = "//*[@id=\"dnn_ctr81355_StateNetDB_UpdatePanel1\"]/div[1]/div[2]/span/label"
states_xpath = "//*[@id=\"dnn_ctr81355_StateNetDB_UpdatePanel1\"]/div[2]/div[2]/span/label"
browser.get(url)
time.sleep(30)

elem = browser.find_element_by_xpath(topics_xpath)
elem.click()
time.sleep(5)
elem = browser.find_element_by_xpath(states_xpath)
elem.click()
Run Code Online (Sandbox Code Playgroud)

但我收到此错误:

ElementClickInterceptedException:消息:元素点击被拦截:
元素 <label for="dnn_ctr81355_StateNetDB_ckBxAllTopics">...</label> 在点 (259, 665) 处不可点击。
其他元素将收到点击:
<label for="dnn_ctr81355_StateNetDB_ckBxTopics_0">...</label>
(会话信息:chrome=76.0.3809.100)

将被单击的复选框位于我要单击的复选框的正下方。

fri*_*anH 6

您需要WebDriverWait确定该元素visibility_of_element_located,然后滚动到Searchable Database该部分,然后您可以使用 locator by xpath

请导入:

from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
Run Code Online (Sandbox Code Playgroud)

尝试下面的代码。

chromedriver_path = r"C:\Users\path\to\chromedriver.exe"
browser = webdriver.Chrome(executable_path=chromedriver_path)
url = "http://www.ncsl.org/research/transportation/autonomous-vehicles-legislative-database.aspx"

topics_xpath = "//div[@class='divTopicsSection1']//span//label[text()='All Topics']"
states_xpath = "//div[@class='divStatesSection1']//span//label[text()='All States']"
dBase_xpath = "//h4[text()='Searchable Database']"
browser.get(url)
WebDriverWait(browser, 10).until(expected_conditions.visibility_of_element_located((By.XPATH, topics_xpath)))
elem = browser.find_element_by_xpath(dBase_xpath)
browser.execute_script("arguments[0].scrollIntoView(true);", elem)

browser.find_element_by_xpath(topics_xpath).click()
browser.find_element_by_xpath(states_xpath).click()
Run Code Online (Sandbox Code Playgroud)