【问题标题】:In python selenium, how does one find the visibility of an element?在 python selenium 中,如何找到元素的可见性?
【发布时间】:2013-04-10 22:57:00
【问题描述】:

我找到了is_visible method in the Selenium documentation,但我不知道如何使用它。我不断收到诸如is_visible needs a selenium instance as the first parameter之类的错误。

另外,什么是“定位器”?

任何帮助将不胜感激。

【问题讨论】:

    标签: python python-2.7 selenium-webdriver


    【解决方案1】:

    您应该改用is_displayed()

    from selenium import webdriver
    
    driver = webdriver.Firefox()
    driver.get('http://www.google.com')
    element = driver.find_element_by_id('gbqfba') #this element is visible
    if element.is_displayed():
      print "Element found"
    else:
      print "Element not found"
    
    hidden_element = driver.find_element_by_name('oq') #this one is not
    if hidden_element.is_displayed():
      print "Element found"
    else:
      print "Element not found"
    

    【讨论】:

      【解决方案2】:

      从这里:https://www.selenium.dev/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html

      随便用

      selenium.webdriver.support.expected_conditions.visibility_of

      cos

      可见性意味着元素不仅被显示,而且具有 大于0的高度和宽度

      【讨论】:

      • visibility_of 内部使用 is_displayed() 所以我看不出您的答案与接受的答案有何不同。并且 OP 并没有询问如何等待满足条件。
      • @MikeScotty 阅读了我发布的链接,并且会理解 :)
      • 我做的还不止这些,我看了implementation - visibility_of 内部uses is_displayed() 点我的链接就明白了。
      • @MikeScotty,让我澄清一下。 is_displayed() 将返回布尔条件是元素是否显示。这并不意味着您可以使用该元素。例如 click(),获取文本等。对我来说,最好的方法是使用 visibility_of_element_located cos Visibility means that the element is not only displayed but also has a height and width that is greater than 0. 这意味着,可以与该元素进行交互。
      • 请不要重复同样的东西,看看代码。 visibility_of 调用 _element_if_visible,后者调用 element.is_displayed()。它只有 5 行代码。我从来没有说过is_displayed() 不依赖检查元素的宽度/高度来确定返回值。实际上is_displayed() 将执行this piece of JS - 所以很难说出实际检查了什么。
      猜你喜欢
      • 1970-01-01
      • 2018-11-16
      • 2021-09-05
      • 2023-03-07
      • 2016-07-18
      • 1970-01-01
      • 2021-08-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多