尝试使用selenium和python登录网页时出错 [英] Error trying to login to webpage using selenium with python

查看:91
本文介绍了尝试使用selenium和python登录网页时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个元素不可见错误:

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

对于运行此代码时的每个find元素行:

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()

页面登录部分的HTML如下:

<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's 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()'>

解决方案

Selenium与用户与Web浏览器的交互方式类似.因此,如果您要尝试与之交互的html元素不可见,那么最简单的解释是,当您编写硒代码时,您不会像普通用户那样与网页进行交互.

最后,这与网页的html无关,而与DOM和元素的hidden属性有关.我建议您下载Firebug或其他一些html查看器程序,然后突出显示要按下的按钮.对html查看器使用DOM查找,并手动完成登录过程.注意您必须要做的是使该元素可见以便与之交互,然后模仿硒代码中的相同步骤.

如果您确实做了所有必要的事情,但是硒与网页的交互速度比javascript使元素可见的速度快,那么您就需要进行编程了. /p>

幼稚的方式:

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

I get an element isn't visible error:

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

For every find element line 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 login section of the page looks like this:

<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's 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()'>

解决方案

Selenium interacts with the web browser in a similar way that the user would. So if there is an html element you're trying to interact with that is not visible then the simplest explanation is that when youre writing your selenium code you're not interacting with the web page like a normal user would.

In the end this isn't about the html of your web page its about the DOM and an element's hidden attribute. I suggest you download firebug or some other html viewer program, and then highlight the button you want to press. Use the DOM lookup for the html viewer and go through the sign in process manually. Notice what you have to do to make the element visible in order to interact with it then mimic the same steps in your selenium code.

If it is a matter of the fact that you did everything you needed to do, but selenium is interacting with the web page faster than the javascript will make the element visible then there is a wait that you have to programmed in.

Naive way:

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

More scalable manner:

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

这篇关于尝试使用selenium和python登录网页时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆