简体   繁体   English

如何使用Selenium登录Yahoo Mail

[英]How to login into Yahoo Mail using Selenium

I am recently working through examples in Automate the Boring Stuff in Python, and came across this example which I have some issues with. 我最近在使用Python自动化无聊的东西的例子,并遇到了这个我遇到一些问题的例子。 In this exercise, I tried to log in to Yahoo Mail using Selenium on the Chrome browser. 在本练习中,我尝试使用Chrome浏览器上的Selenium登录Yahoo Mail。 The code does not produce any error and successfully prints the last line, however, it fails to click on the submit button which leaves me stuck at the login page despite successfully filling up the password. 代码不会产生任何错误并成功打印最后一行,但是,它无法单击提交按钮,这使得我在登录页面停留,尽管成功填写了密码。 However, if I click on the login button manually, I can login successfully. 但是,如果我手动点击登录按钮,我可以成功登录。

雅虎邮箱登录页面

I have tried both passwordelem.submit() and passwordsubmitbtn.click() but neither works. 我已经尝试了passwordelem.submit()和passwordsubmitbtn.click(),但都不起作用。 I have also tried changing the user-agent in chrome but to no avail. 我也尝试在chrome中更改用户代理但无济于事。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options


browser = webdriver.Chrome()
browser.get('https://login.yahoo.com?.src=ym&.lang=en-US&.intl=us&.done=https%3A%2F%2Fmail.yahoo.com%2Fd')
wait = WebDriverWait(browser, 10)

emailelem = wait.until(EC.presence_of_element_located((By.ID,'login-username')))
emailelem.send_keys('xxxxxx@yahoo.com')

wait.until(EC.presence_of_element_located((By.ID,'login-signin')))
emailelem.submit()

passwordelem = wait.until(EC.presence_of_element_located((By.ID,'login-passwd')))
passwordelem.send_keys('xxxxxx')
##passwordelem.submit()

passwordsubmitbtn = wait.until(EC.element_to_be_clickable((By.ID,'login-signin')))
passwordsubmitbtn.click()

print ('Done')

Try this: 尝试这个:

Add top of the script 添加脚本的顶部

from selenium.webdriver.common.keys import Keys

Replace your code 替换你的代码

passwordelem = wait.until(EC.presence_of_element_located((By.ID,'login-passwd')))
passwordelem.send_keys('xxxxxx')
##passwordelem.submit()

passwordsubmitbtn = wait.until(EC.element_to_be_clickable((By.ID,'login-signin')))
passwordsubmitbtn.click()

To: 至:

wait.until(EC.element_to_be_clickable((By.ID,'login-signin')))
passwordelem = wait.until(EC.presence_of_element_located((By.ID,'login-passwd')))
passwordelem.send_keys('xxxxxxx')
passwordelem.send_keys(Keys.RETURN)

More details 更多细节

To click() on the button with text as Sign in providing valid valid credentials on Yahoo Mail Login Page you have to induce WebDriverWait for the element to be clickable and you can use the following Locator Strategy : click()按钮上的文本为登录,Yahoo Mail登录页面上提供有效的有效凭据,您必须为要点击元素引导WebDriverWait ,您可以使用以下定位策略

  • Code Block: 代码块:

     from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC options = webdriver.ChromeOptions() options.add_argument('start-maximized') #options.add_argument('disable-infobars') options.add_argument('--disable-extensions') driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\\WebDrivers\\chromedriver.exe') driver.get('https://login.yahoo.com') WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.phone-no"))).send_keys("LinX123@yahoo.com") driver.find_element_by_css_selector("input.orko-button-primary.orko-button#login-signin").click() WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#login-passwd"))).send_keys("LinX123@yahoo.com") driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.pure-button.puree-button-primary.puree-spinner-button")))) 
  • Browser Snapshot 浏览器快照

yahoo_login

PS: Note the visible spinner-button highlighted when the login process starts. PS:注意登录过程开始时突出显示的可见 微调按钮

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM