I'm getting this error when I run this Python/Selenium script.
File "./a.py", line 21, in <module>
elem = driver.find_element_by_id("licensees").click()
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message:
Here is the html code
<p>
<button onClick="myloginwindow1('')" value="Login Now" name="licensees" id="licensees">
<p>Licensee Login</p>
</button>
</p>
Here is the code.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("https://xxxxx.com")
assert "xxxxxxxx" in driver.title
try:
element = WebDriverWait(driver, 30).until(
EC.presence_of_element_located((By.ID, "licensees"))
)
elem = driver.find_element_by_id("licensees").click()
finally:
driver.quit()
When I remove
elem = driver.find_element_by_id("licensees").click()
I don't get an error.
3 Answers 3
You can also execute the script that's executed when clicking the button. This way you won't need to wait for the element to be clickable.
driver.execute_script(
"myloginwindow1('')"
)
Comments
Use visibilityOfElementLocated
instead of presence_of_element_located
presenceOfElementLocated
don't care whether if element visible or not, It just checks element is on the page
try:
WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.ID, "licensees"))).click()
Comments
As mentioned elsewhere, presence is different than visibility.
But with capybara-py, you don’t have to think about either:
from capybara.dsl import page
page.visit("...")
page.assert_title("...")
page.click_button("Licensee Login")
Here, click_button()
waits for the button to be interactable.
(Similarly, assert_title()
waits for the title to match, in case, e.g., the page takes a while to fully load.)