ElementClickInterceptedException:Message:Element Click Intercepted:Element<Label&>无法使用Selenium和Python单击 [英] ElementClickInterceptedException: Message: element click intercepted: Element <label> is not clickable with Selenium and Python

查看:46
本文介绍了ElementClickInterceptedException:Message:Element Click Intercepted:Element<Label&>无法使用Selenium和Python单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试单击"所有主题"和"所有州"复选框,然后搜索结果。当我运行该脚本时,一个大小为1036x674的铬色窗口打开。

如果不使用窗口,则会出现元素单击拦截错误。如果最小化或最大化窗口,我的脚本运行正常。

我使用的是Selenium 3.141.0、Chrome 76、chromeDriver 76和Python 3.6

chromedriver_path = r"C:Userspath	ochromedriver.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:Message:Element Click Intercepted:
元素<;标签for="dnn_ctr81355_StateNetDB_ckBxAllTopics">.<;/label>在点(259665)处不可单击。
其他元素将收到点击:
<;Label for="dnn_ctr81355_StateNetDB_ckBxTopics_0">.<;/label>
(会话信息:Chrome=76.0.3809.100)

要单击的复选框就在我尝试单击的复选框的正下方。

推荐答案

您需要WebDriverWait确定元素visibility_of_element_located,然后滚动到Searchable Database节,xpath即可使用Locator Byxpath

请导入:

from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

尝试以下代码。

chromedriver_path = r"C:Userspath	ochromedriver.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()

这篇关于ElementClickInterceptedException:Message:Element Click Intercepted:Element&amp;lt;Label&amp;>无法使用Selenium和Python单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆