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.
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.
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"
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()
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
visibility_of
internally uses is_displayed()
Click my link and you will understand. –
Angele 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 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 import selenium.webdriver.remote.webelement as w;print(w.isDisplayed_js)
–
Angele 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.
visibility_of
internally usesis_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