In python selenium, how does one find the visibility of an element?

PythonPython 2.7Selenium Webdriver

Python Problem Overview


I found the is_visible method in the Selenium documentation, but I have no idea how to use it. I keep getting errors such as is_visible needs a selenium instance as the first parameter.

Also, what is a "locator"?

Any help would be appreciated.

Python Solutions


Solution 1 - Python

You should use is_displayed() instead:

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"

Solution 2 - Python

From here: https://www.selenium.dev/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html

Just use

> selenium.webdriver.support.expected_conditions.visibility_of

cos

> Visibility means that the element is not only displayed but also has a > height and width that is greater than 0

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2183536View Question on Stackoverflow
Solution 1 - PythonrcdsystemsView Answer on Stackoverflow
Solution 2 - PythonZhivko.KostadinovView Answer on Stackoverflow