Error trying to login to webpage using selenium with python

I get an element with no visible error:

ElementNotVisibleException: Message: u'Element is not currently visible and so may 
not be interacted with' 

For each line of the search item, when I run this code:

from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.example.com')
browser.find_element_by_name('username').send_keys('myusername')
browser.find_element_by_name('password').send_keys('mypassword')
browser.find_element_by_class_name('welcomeLoginButton').click()

The HTML for the page login section is as follows:

<div class='welcomeLoginUsername'>
<div class='welcomeLoginUsernameLabel'><b>Username:</b></div>
<div class='welcomeLoginUsernameInput'><input type='text' name='username' tabindex='1'>
<br><a class='sf' href='javascript: void(0);' onclick='showUsernamePopup();'>
<b>Forgot Username?</b></a>
</div>
</div>
<div class='welcomeLoginPassword'>
<div class='welcomeLoginPasswordLabel'>
<b>Password:</b>
<br><span class='sf'>(It cAsE sEnSitIvE!)</span>
</div>
<div class='welcomeLoginPasswordInput'>
<input type='password' name='password' tabindex='2'>
<br><a class='sf' href="javascript: void(0);" onclick="showPasswordPopup();">
<b>Forgot Password?</b></a>
</div>
</div>
</div>
<input type="submit" value="" class='welcomeLoginButton' style='border: 0px; 
padding: 0px; margin: 0px;) no-repeat;' onclick='document.forms["login"].submit()'>
+5
source share
1 answer

Selenium interacts with a web browser in the same way as a user. Therefore, if there is an html element that you are trying to interact with, this is not visible, so the simplest explanation is that when you write your selenium code, you are not interacting with the web page, as usual.

, html - DOM . firebug html, , . DOM- html- . , , , , .

, , , - , javascript , , .

:

import time
time.sleep(1) # this is done in seconds

:

import time

welcome_button = browser.find_element_by_class_name('welcomeLoginButton')

wait_for_element_visibility(welcome_button).click()

def wait_for_element_visibility(element):
   if element.is_visible():
      return element
   else:
      for i in range(10):
         if not element.is_visible():
            time.sleep(.5)
         else:
            return element
+6

All Articles