在python selenium中,如何找到元素的可见性?

时间:2013-04-10 22:57:00

标签: python-2.7 selenium-webdriver

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

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

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:74)

您应该使用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"

答案 1 :(得分:0)

从这里: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