【问题标题】:Python Selenium Webdriver element found but can not click on it找到 Python Selenium Webdriver 元素但无法单击它
【发布时间】:2018-04-12 23:48:24
【问题描述】:

目标是单击登录页面上的复选框。 我通过 XPATH 找到了该元素,但无法点击它。

>>> elem = driver.find_element_by_xpath("//input[@type='checkbox'][@name='conditions']")
>>> elem.is_displayed()
False
>>> elem.is_enabled()
True
>>> elem.get_attribute('outerHTML')
u'<input type="checkbox" class="custom-control-input" name="conditions">'

当我尝试elem.click() 时,出现异常: selenium.common.exceptions.ElementNotVisibleException: Message: element not visible 但元素清晰可见,因为页面已加载并且我从终端工作。

当我使用不同的选择器时的其他错误是: driver.find_element_by_xpath('/html/body/div[2]/main/section/div/div[3]/div/div[1]/form/p[1]/label/input').click()

selenium.common.exceptions.WebDriverException: Message: unknown error: Element &lt;input type="checkbox" class="custom-control-input" name="conditions"&gt; is not clickable at point (51, 549). Other element would receive the click: &lt;span class="custom-control-description font-weight-regular"&gt;...&lt;/span&gt;

我尝试注入 JavaScript,但没有成功。 driver.execute_script("arguments[0].style.visibility = 'visible';",elem)

任何想法如何解决这个问题?

【问题讨论】:

  • 尝试显式等待元素的可见性然后点击它。

标签: javascript python selenium checkbox webdriver


【解决方案1】:

根据您尝试时的代码试验:

elem.click()

你看到了:

selenium.common.exceptions.ElementNotVisibleException: Message: element not visible

这意味着所需的元素在HTML DOM 中仍然不可见

甚至在你尝试点击之前:

elem.is_displayed()

你看到了:

False

但是当你尝试时:

elem.is_enabled()

你看到了:

True

因此结合所有这些观察结果,它可以是任何一种情况:

  • 元素在 DOM 中存在,但仍然不可见/不可交互。在这种情况下,您需要诱导WebDriverWait,然后调用click(),如下所示:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@type='checkbox' and @name='conditions']"))).click()
    
  • 元素在 DOM 中存在,但不在Viewport 中。在这种情况下,您需要调用execute_script() 将元素带入视口,然后调用click(),如下所示:

    elem = driver.find_element_by_xpath("//input[@type='checkbox' and @name='conditions']")
    driver.execute_script("arguments[0].scrollIntoView(true);", elem)
    elem.click()
    
  • 您调整的 Locator Strategy 有可能不是唯一的,它会识别多个 WebElements,而要识别的第一个元素可能会被隐藏。在这种情况下,is_displayed() 将始终返回 False,并且您必须构造一个唯一的 Locator Strategy 来唯一标识预期的元素。
  • 元素的 style 属性有可能设置为 display: none;,在这种情况下,您必须使用execute_script() 方法,如下所示:

    elem = driver.find_element_by_xpath("//input[@type='checkbox' and @name='conditions']")
    driver.execute_script("arguments[0].removeAttribute('style')", elem)
    elem.click()
    

【讨论】:

  • 感谢@DebanjanB 很好的答案,我尝试并仔细检查了所有内容,但总是得到错误:Element ... is not clickable at point ...selenium.common.exceptions.WebDriverException: Message: unknown error: Element &lt;input type="checkbox" class="custom-control-input" name="conditions"&gt; is not clickable at point (51, 286). Other element would receive the click: &lt;span class="custom-control-description font-weight-regular"&gt;...&lt;/span&gt;
【解决方案2】:

你可以使用这个 XPATH :- //input[@type='checkbox'and @name='conditions']

对于以下错误:- 在点击事件之前使用等待

selenium.common.exceptions.WebDriverException:消息:未知错误:元素在点 (51, 549) 处不可点击。其他元素会收到点击:...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-31
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多