login to yahoo email account using Python Selenium

2019-07-08 09:56发布

问题:

I need to login to yahoo email account using Selenium with Python.

this is my code

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("https://login.yahoo.com")

print driver.current_url

logintxt = driver.find_element_by_name("username")
logintxt.send_keys("email")

pwdtxt = driver.find_element_by_name("passwd")
pwdtxt.send_keys("pass")



button = driver.find_element_by_id("login-signin")
button.click()
driver.get("https://mail.yahoo.com")
print driver.current_url

but when I print the current url, it always gives me the login page, which mean that it didn't login.

any idea about how to fix it ? I'm using Centos 6 with python 2.6

回答1:

Wait for it (using WebDriverWait) to redirect you to the yahoo main page on successful login before navigating to the Yahoo mail box:

from selenium.webdriver.support.wait import WebDriverWait

button = driver.find_element_by_id("login-signin")
button.click()

# give it time to log in
wait = WebDriverWait(driver, 10)
wait.until(lambda driver: driver.current_url == "https://www.yahoo.com/")

driver.get("https://mail.yahoo.com")