ElementClickInterceptedException:消息:拦截了元素单击:Selenium和Python无法单击元素<label>

时间:2019-08-23 20:18:48

标签: python selenium xpath css-selectors webdriverwait

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

如果我不理会窗口,则会出现元素点击拦截错误。如果我最小化或最大化窗口,则脚本可以正常工作。

我正在使用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()

但我收到此错误:

  

ElementClickInterceptedException:消息:拦截了元素单击:
  元素在点(259,665)不可单击。
  其他元素将获得点击:
  
    (会话信息:chrome = 76.0.3809.100)

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

2 个答案:

答案 0 :(得分:0)

要在与{strong>所有主题和所有状态的文本相关联的复选框上click(),您必须为{引出 WebDriverWait {1}},您可以使用以下任何Locator Strategies

  • 使用element_to_be_clickable()

    CSS_SELECTOR
  • 使用driver.get("http://www.ncsl.org/research/transportation/autonomous-vehicles-legislative-database.aspx") WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label[for$='_StateNetDB_ckBxAllTopics']"))).click() driver.find_element_by_css_selector("label[for$='_StateNetDB_ckBxAllStates']").click()

    XPATH
  • 浏览器快照:

checkboxes

答案 1 :(得分:0)

您需要#include <boost/spirit/home/x3.hpp> #include <iostream> #include <vector> //your attribute, could be more complex, might use namespace using attr = std::vector<std::string>; namespace parser { namespace x3 = boost::spirit::x3; const auto fn_name = +x3::char_("a-zA-Z"); const auto string = +x3::char_("a-zA-Z_0-9"); const auto start = x3::rule<struct _, attr>() = fn_name >> "(" >> string % ',' >> ")"; } int main() { namespace x3 = boost::spirit::x3; std::string cmd("fun(1,.2,3,4 , 5, foo) "); attr VV; auto it = cmd.begin(); bool result = phrase_parse(it, cmd.end(), parser::start, x3::space, VV); if (result) { for (auto sss : VV) { std::cout << "-> " << sss << std::endl; } } else std::cout << "Fail at" << std::endl; return 0; } 来确保元素WebDriverWait,然后滚动到visibility_of_element_located部分,然后才能使用Searchable Database的定位符。

请导入:

xpath

尝试下面的代码。

from selenium.webdriver.support import expected_conditions
相关问题