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

3

61

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.

Nealneala answered 10/4, 2013 at 22:57 Comment(0)
A
131

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"
Abydos answered 11/4, 2013 at 4:30 Comment(0)
G
3

Here is the script that works for me in python3:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

ser = Service(r"C:/Users/geckodriver.exe")
driver = webdriver.Firefox(service=ser)

# should open Firefox

driver.get("https://example.com")

# should display home page

try:
    element = driver.find_element(By.ID, 'footer-block')
    if element.is_displayed():
        break

except:
    continue

driver.close()
Gert answered 7/9, 2023 at 9:3 Comment(0)
S
1

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

Sesquicarbonate answered 27/11, 2020 at 9:18 Comment(7)
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.Angele
@MikeScotty read the link which I posted, and will understand :)Sesquicarbonate
I did more than that, I looked at the implementation - visibility_of internally uses is_displayed() Click my link and you will understand.Angele
@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.Sesquicarbonate
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 - so it's hard to tell what's actually checked.Angele
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) Angele
Thanks @Zhivko.Kostadinov, this is exactly what I needed since I wanted to employ Selenium's WebDriverWait along with an expected condition while not having a locator but the actual web element at hand: target_web_element = WebDriverWait(self._driver, MAX_WAIT_TIME_SECONDS).until(expected_conditions.visibility_of(target_web_element), _get_webdriver_wait_error_msg)Palaeo

© 2022 - 2024 — McMap. All rights reserved.