54

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.

User
  • 1,098
  • 9
  • 28
user2183536
  • 545
  • 1
  • 4
  • 5

2 Answers2

123

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"
lmiguelvargasf
  • 51,786
  • 40
  • 198
  • 203
rcdsystems
  • 1,458
  • 1
  • 10
  • 11
0

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

  • 1
    ``visibility_of`` internally uses ``is_displayed()`` so I don't see how your answer is different from the accepted answer. And OP is not asking how to wait for a condition to be met. – Mike Scotty Jul 15 '21 at 09:39
  • @MikeScotty read the link which I posted, and will understand :) – Zhivko.Kostadinov Jul 15 '21 at 10:41
  • I did more than that, I looked at the [implementation](https://www.selenium.dev/selenium/docs/api/py/_modules/selenium/webdriver/support/expected_conditions.html#visibility_of) - ``visibility_of`` internally ``uses is_displayed()`` Click my link and you will understand. – Mike Scotty Jul 15 '21 at 14:35
  • @MikeScotty , let me clarify. `is_displayed()` will return Boolean condition is that element is displayed or not. That does not mean, you could use that element. e.g click(), get text and so on. For me, the best way is to use `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.` Which means, could interact with that element. – Zhivko.Kostadinov Jul 16 '21 at 11:40
  • Will you please stop repeating the same stuff and just look at the code. ``visibility_of`` calls ``_element_if_visible`` which calls ``element.is_displayed()``. It's just 5 lines of code. I never said that ``is_displayed()`` does not rely on checking width/height of the element to determine the return value. Actually ``is_displayed()`` will execute [this piece of JS](https://github.com/SeleniumHQ/selenium/blob/trunk/rb/lib/selenium/webdriver/atoms/isDisplayed.js) - so it's hard to tell what's actually checked. – Mike Scotty Jul 16 '21 at 12:57
  • Proof that this piece of JS is actually part of selenium's python distribution: ``import selenium.webdriver.remote.webelement as w;print(w.isDisplayed_js) `` – Mike Scotty Jul 16 '21 at 13:05