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

查看:554
本文介绍了python和selenium实现outlook.com登录脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

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)

谢谢大家啦!

解决方案

输入密码之所以不行,是因为密码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()

这篇关于python和selenium实现outlook.com登录脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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