Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Selenium is_displayed() returns true and ElementNotVisible exception is still raised?

Before calling any of the element's send_keys(), I first check if it's enabled and visible so it doesn't raise an exception.

What happens is is_Displayed returns True and when I try to send_keys to that element it still raises an exception of ElementNotVisible. Is this some form of a bug?

It works on some websites, it doesn't work on another.

def login():
 elem = browser.find_elements_by_xpath('//input[contains(@name, "user")]')
 for elements in elem:
  if elements.is_displayed():
   if elements.is_enabled():
    elements.send_keys(username)
    elem = browser.find_elements_by_xpath('//input[contains(@name, "pass")]')
    for elements in elem:
     if elements.is_displayed():
       if elements.is_enabled():
        elements.clear()
        elements.send_keys(password + Keys.RETURN)   #Crashes here
        time.sleep(4)
        return
like image 690
InstallGentoo Avatar asked Jul 02 '13 18:07

InstallGentoo


People also ask

Is displayed () in Python?

the is_displayed() method returns a boolean value specifying whether the target element is displayed or not displayed on the web page. The preceding code uses the isDisplayed() method to determine if the element is displayed on a web page. The preceding code returns true for the Google Search button.

Is enabled method in selenium Python?

isEnabled() Method in Selenium isEnabled() method is used to check if the web element is enabled or disabled within the web page. This method returns “true” value if the specified web element is enabled on the web page otherwise returns “false” value if the web element is disabled on the web page.


2 Answers

Try this:

def login():
 user_elements = browser.find_elements_by_xpath('//input[contains(@name, "user")]')
 for user in user_elements:
  if user.is_displayed():
   if user.is_enabled():
    user.send_keys(username)
    pass_elements = browser.find_elements_by_xpath('//input[contains(@name, "pass")]')
    for passw in pass_elements:
     if passw.is_displayed():
       if passw.is_enabled():
        passw.clear()
        passw.send_keys(password + Keys.RETURN)   #Crashes here
        time.sleep(4)
        return

It's likely your choice of variable names make you clobber the outside loop with the inside loop.

like image 74
Belrog Avatar answered Sep 23 '22 03:09

Belrog


If anyone is still wondering what problem was, it was caused by javascript hiding element after page was fully loaded.

Completely disabling javascript on page solved that issue.

like image 32
InstallGentoo Avatar answered Sep 26 '22 03:09

InstallGentoo