【问题标题】:Element identified through find_element_by_xpath returns selenium.common.exceptions.ElementNotVisibleException: Message: element not visible通过 find_element_by_xpath 标识的元素返回 selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
【发布时间】:2018-10-15 09:34:51
【问题描述】:

点击按钮有问题。如果敌人在场,这个按钮可以点击,如果敌人出去,则不能点击 开始 我试过这个:

try:
    element= driver.find_element_by_xpath("//button[@class='butwb']")
    if element.is_displayed():       
        print ("Element found")
    else:
        print ("Element not found")
except NoSuchElementException:
    print("No element found") 

结果:

Element not found

如果我添加element.click()

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

我做错了什么?

【问题讨论】:

  • 您找到了一个不可见的元素。你不一定做错了什么。如果您尝试等待并且它从未变得可见,那么它可能永远不会。您可能正在识别错误​​的元素。 Selenium 被设计为不允许与不可见元素交互(模拟用户)。我建议您使用 $x() 在浏览器开发控制台中测试您的定位器。
  • @JeffC 可以私信你吗?
  • 你可以在这里联系我。
  • 我不能对代码做太多事情。我需要的部分是指向该站点的链接以及您尝试单击的按钮的说明。

标签: python selenium-webdriver


【解决方案1】:

此错误消息...

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

...暗示您尝试与之交互的元素不可见

以下用于识别元素的代码行成功,因为该元素存在于HTML DOM 中:

element= driver.find_element_by_xpath("//button[@class='butwb']")

此时值得一提的是,元素的存在意味着该元素存在于页面的DOM中。这并不保证元素是可见(即显示)可交互(即可点击)

可见性表示元素不仅被显示,而且高度和宽度都大于0。

因此 is_displayed() 方法返回 false 并执行 else{} 块并打印:

Element not found

当您进一步调用 click() 时,会引发以下异常:

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

可能的原因

  • 您采用的定位器策略无法识别元素,因为它不在浏览器的Viewport内。

  • 您采用的 Locator Strategy 并没有唯一地 识别 HTML DOM 中的所需元素,目前还可以找到其他一些元素隐藏 / 不可见元素。

  • 您采用的 Locator Strategy 标识了元素,但由于存在属性 style="display: none;" 而不可见。

  • WebElement 可能出现在 模态对话框 中,并且可能不会立即可见/可交互。

解决方案

  • 使用execute_script()方法滚动元素查看如下:

    elem = driver.find_element_by_xpath("element_xpath")
    driver.execute_script("arguments[0].scrollIntoView();", elem);
    

    这里有关于Scrolling to top of the page in Python using Selenium的详细讨论

  • 如果您采用的 Locator Strategy 不能唯一标识 HTML DOM 中的所需元素,并且当前定位了其他一些 隐藏 / 不可见元素 元素作为第一个匹配项,您必须更改 Locator Strategy

  • Incase 元素具有 style="display: none;" 属性,通过executeScript() 方法删除该属性,如下所示:

    elem = driver.find_element_by_xpath("element_xpath")
    driver.execute_script("arguments[0].removeAttribute('style')", elem)
    elem.send_keys("text_to_send");
    
  • 如果元素在 HTML DOM 中不存在/可见/可交互,则立即诱导@987654325 @ 和 expected_conditions 设置为正确的方法如下:

    • 等待presence_of_element_located

      element = WebDriverWait(driver, 20).until(expected_conditions.presence_of_element_located((By.XPATH, "element_xpath']")))
      
    • 等待visibility_of_element_located

      element = WebDriverWait(driver, 20).until(expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, "element_css")
      
    • 等待element_to_be_clickable

      element = WebDriverWait(driver, 20).until(expected_conditions.element_to_be_clickable((By.LINK_TEXT, "element_link_text")))
      

【讨论】:

  • 当“等待 visibility_of_element_located :”引发 TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:
  • @Kuncioso 您是否尝试 scrollIntoView() 元素或检查 attribute style="display: none;" 或元素是否隐藏的元素?
【解决方案2】:

与 Kuncioso 交谈并进入游戏后,我们发现有两个按钮与他的定位器匹配,第一个按钮被隐藏了。使用下面的代码点击第二个按钮,即他想要的那个,问题就解决了。

driver.find_elements_by_xpath("//button[@class='butwb']")[1].click()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-17
    • 1970-01-01
    • 1970-01-01
    • 2019-04-01
    • 2021-12-25
    • 1970-01-01
    • 2014-07-23
    相关资源
    最近更新 更多