【发布时间】: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
我找到了is_visible method in the Selenium documentation,但我不知道如何使用它。我不断收到诸如is_visible needs a selenium instance as the first parameter之类的错误。
另外,什么是“定位器”?
任何帮助将不胜感激。
【问题讨论】:
标签: python python-2.7 selenium-webdriver
您应该改用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"
【讨论】:
随便用
selenium.webdriver.support.expected_conditions.visibility_of
cos
可见性意味着元素不仅被显示,而且具有 大于0的高度和宽度
【讨论】:
visibility_of 内部使用 is_displayed() 所以我看不出您的答案与接受的答案有何不同。并且 OP 并没有询问如何等待满足条件。
visibility_of 内部uses is_displayed() 点我的链接就明白了。
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 - 所以很难说出实际检查了什么。