【问题标题】:ElementClickInterceptedException: Message: element click intercepted: Element <label> is not clickable with Selenium and PythonElementClickInterceptedException:消息:元素点击被拦截:元素 <label> 不能使用 Selenium 和 Python 点击
【发布时间】:2019-12-29 03:34:39
【问题描述】:

我正在尝试单击“所有主题”和“所有状态”复选框,然后搜索结果。当我运行脚本时,会打开一个大小为 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()

但我得到这个错误:

ElementClickInterceptedException:消息:元素点击被拦截:
元素 在点 (259, 665) 处不可点击。
其他元素会收到点击:

(会话信息:chrome=76.0.3809.100)

将被点击的复选框就在我试图点击的复选框的正下方。

【问题讨论】:

    标签: python selenium xpath css-selectors webdriverwait


    【解决方案1】:

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

    请导入:

    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:\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()
    

    【讨论】:

      【解决方案2】:

      此错误消息...

      ElementClickInterceptedException: Message: element click intercepted
      

      ...暗示在所需元素上调用的 click 方法被其他元素拦截


      click() 在与文本关联的复选框上作为All TopicsAll States 您必须为element_to_be_clickable() 诱导WebDriverWait您可以使用以下任一Locator Strategies

      • 使用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:

        driver.get("http://www.ncsl.org/research/transportation/autonomous-vehicles-legislative-database.aspx")
        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[contains(@for, '_StateNetDB_ckBxAllTopics')]"))).click()
        driver.find_element_by_xpath("//label[contains(@for, '_StateNetDB_ckBxAllStates')]").click()
        
      • 浏览器快照:

      【讨论】:

      • 谢谢,我不知道有办法等到元素可点击!
      • @William 很高兴能够为您提供帮助,但是当您打算点击 element is clickable 是正确的方式但不是 visibility_of_element_located
      猜你喜欢
      • 2020-01-04
      • 2021-10-08
      • 2019-11-08
      • 1970-01-01
      • 2021-09-04
      • 2019-12-13
      • 2021-05-16
      • 2021-08-26
      • 2021-04-17
      相关资源
      最近更新 更多