首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ElementClickInterceptedException:消息:元素单击被截获:元素<label>不能用Selenium和Python点击

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

Stack Overflow用户
提问于 2019-08-23 20:18:48
回答 2查看 32.2K关注 0票数 7

我试图点击“所有主题”和“所有国家”CheckBoxes,然后搜索结果。当我运行脚本时,将打开一个大小为1036x674的chrome窗口。

如果不使用该窗口,则会得到元素单击“拦截错误”。如果我最小化或最大化窗口,我的脚本工作良好。

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

代码语言:javascript
复制
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()

但我知道这个错误:

ElementClickInterceptedException:消息:元素单击“截获”: 元素..。不能点击点(259,665)。 其他元素将收到单击: ..。 (会议信息: chrome=76.0.3809.100)

将被点击的CheckBox就在我试图点击的那个下面。

EN

Stack Overflow用户

回答已采纳

发布于 2019-08-23 23:25:18

您需要WebDriverWait来确保元素visibility_of_element_located,然后滚动到Searchable Database部分,您可以使用xpath定位器。

请进口:

代码语言:javascript
复制
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

试试下面的代码。

代码语言:javascript
复制
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()
票数 4
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57632563

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档