python和selenium实现outlook.com登录脚本

吃鱼不吃鱼尾巴
  • 34

hello,there
我想用python和selenium实现outlook.com的自动登录脚本。在输入邮箱验证部分是没问题的,但是到了输入密码的时候总是提示element not invisible。
我尝试了很多种办法都不行,也尝试从头开始写xpath,都不行。
很奇怪的是,输入账号和密码的过程应该是一模一样的,没道理输入密码的时候不行。。。
代码很短,没几行谁帮着瞅瞅:

browser = webdriver.Chrome()
# logout for 1st step
#browser.get('https://outlook.live.com/owa/logoff.owa')
browser.get('https://login.live.com/login.srf?&wreply=https%3a%2f%2foutlook.live.com%2fowa%2f%3fnlp%3d1%26realm%3dlogin.live.com')

username = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.XPATH, "//input[@id='i0116']")))
username.clear()
username.send_keys(usernameStr)
nextButton = browser.find_element_by_id('idSIButton9')
nextButton.click()

password = WebDriverWait(browser, 50).until(
EC.presence_of_element_located((By.XPATH, "//input[@id='i0118']")))
password.clear()
password.send_keys(passwordStr)
signinButton = browser.find_element_by_id('idSIButton9')
signinButton .click()

报的错误是:

Traceback (most recent call last):
  File "test.py", line 29, in <module>
    browser.find_element_by_id('i0118').click()
  File "C:\Python27\lib\site-packages\selenium-3.0.2-py2.7.egg\selenium\webdrive
r\remote\webelement.py", line 77, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Python27\lib\site-packages\selenium-3.0.2-py2.7.egg\selenium\webdrive
r\remote\webelement.py", line 494, in _execute
    return self._parent.execute(command, params)
  File "C:\Python27\lib\site-packages\selenium-3.0.2-py2.7.egg\selenium\webdrive
r\remote\webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium-3.0.2-py2.7.egg\selenium\webdrive
r\remote\errorhandler.py", line 192, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not visi
ble
  (Session info: chrome=55.0.2883.87)
  (Driver info: chromedriver=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cf
d9),platform=Windows NT 6.1.7601 SP1 x86)

谢谢大家啦!

回复
阅读 5.2k
2 个回答
oliver_lv
  • 1.7k
✓ 已被采纳

输入密码之所以不行,是因为密码input一开始是隐藏的,就算你跳转了,页面上可见了,但是browser = webdriver.Chrome()的browser里的page_source中密码框还是不可见的。所以你需要分析页面元素,看看密码框的class前后值。所以需要执行一段JS脚本让密码框可见。browser.execute_script("document.getElementById('i0118').setAttribute('class', 'form-control')")我修改了下你的代码,你可以参考下。此外也需要对登录按钮进行处理。因为一开始登录按钮的值是"下一步"且是disabled。碰到这类问题一定要去看页面,具体分析。 具体看代码

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
import time

browser = webdriver.Chrome()
# logout for 1st step
#browser.get('https://outlook.live.com/owa/logoff.owa')
browser.get('https://login.live.com/login.srf?&wreply=https%3a%2f%2foutlook.live.com%2fowa%2f%3fnlp%3d1%26realm%3dlogin.live.com')

username = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.XPATH, "//input[@id='i0116']")))
username.clear()
username.send_keys("your email")
nextButton = browser.find_element_by_id('idSIButton9')
nextButton.click()
time.sleep(3)

browser.execute_script("document.getElementById('i0118').setAttribute('class', 'form-control')")
password = WebDriverWait(browser, 50).until(
EC.presence_of_element_located((By.XPATH, "//input[@id='i0118']")))
password.clear()
password.send_keys("your password")
browser.execute_script("document.getElementById('idSIButton9').disabled=false")
signinButton = browser.find_element_by_id('idSIButton9')
signinButton.send_keys(u"登录")
signinButton.click()
time.sleep(5)
browser.close()
yabinzhang
  • 3
新手上路,请多包涵

我把楼主的代码一行一行贴到ipython中执行,并没有遇到元素不可见的问题。
根据楼主贴出来的tb,报错的是test.py文件29行的browser.find_element_by_id('i0118').click()
但这一行并没有出现在楼主贴出的代码中。尴尬了...楼主还是要贴报错的源码哈。
在报错这句前,应该没有显示的等待或者WebDriverWait,我猜。

之所以报这个错误selenium.common.exceptions.ElementNotVisibleException,字面意思理解,是因为渲染出的页面中无法定位这个可见的元素。原因可能会包括元素本身不可见,或者页面渲染未完成,当然还有其他原因...

轻拍...

宣传栏