2

My question actually is the same as this one login to yahoo email account using Python Selenium webdrive

But since Yahoo has changed its login form UI, the answer provided in the above link doesn't work to me. Instead I tried the below code.

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

browser = webdriver.Firefox()
browser.get('https://login.yahoo.com')
emailElem = browser.find_element_by_id('login-username')
emailElem.send_keys('thisismyemail@yahoo.com')
emailElem.submit()

passwordElem = WebDriverWait(browser, 10).until(
    EC.presence_of_element_located((By.ID, "login-passwd"))
)
passwordElem.send_keys('thisismypassword')
passwordElem.submit()

wait = WebDriverWait(browser, 10)
wait.until(lambda browser: browser.current_url == "https://www.yahoo.com")
browser.get("https://mail.yahoo.com")

When I run the above code, it always gives me the password input page. I executed the code line by line, I think the second wait after password submission didn't work.

Need your advice how to change it for redirecting to the yahoo main page on successful login before navigating to the Yahoo mail box. Thank you!

Follow-up questions: @DebanjanB and @Nimish Bansal, Thanks a lot for your answers! I tried and both of your answers can work. I understand the key difference from my code is to change .submit() to .click(). But why? From the Selenium-Python document(http://selenium-python.readthedocs.io/navigating.html#filling-in-forms), both work in the same way for filling in forms. Any reference to elaborate the difference between .submit() and .click()? Thanks.

Guy
  • 46,488
  • 10
  • 44
  • 88
XiaoShu
  • 65
  • 5

2 Answers2

1

To login into Yahoo through Selenium-Python using a valid set of credentials (username and password) you can use any of the the following block of code using either CSS_SELECTOR or XPATH:

  • CSS_SELECTOR :

    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        
    browser = webdriver.Firefox()
    browser.get('https://login.yahoo.com')
    WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.phone-no#login-username"))).send_keys('my_username@yahoo.co.in')
    browser.find_element_by_id("login-signin").click()
    WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#login-passwd"))).send_keys('my_password')
    browser.find_element_by_id("login-signin").click()
    
  • XPATH :

    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        
    browser = webdriver.Firefox()
    browser.get('https://login.yahoo.com')
    WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='phone-no ' and @id='login-username']"))).send_keys('my_username@yahoo.co.in')
    browser.find_element_by_id("login-signin").click()
    WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='login-passwd']"))).send_keys('my_password')
    browser.find_element_by_id("login-signin").click()
    

Update :

As per your followup Question Any reference to elaborate the difference between .submit() and .click()? here are some details :

submit() works when the elements are within a tag. You would be able to successfully submit the input via submit() as it blocks the method until the new page is loaded completely as a result of submission on the previous page.

But when you try click() method, click() being a native event doesn't waits for the new page to load completely. Hence invoking click() method sometimes fails.

For a detailed analysis you can check this Answer for Selenium: submit() works fine, but click() does not

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0
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

browser = webdriver.Chrome()
browser.get('https://login.yahoo.com')
emailElem = browser.find_element_by_id('login-username')
emailElem.send_keys('email@yahoo.in')
loginbtn=browser.find_element_by_id("login-signin")
loginbtn.click()


passwordElem = WebDriverWait(browser, 10).until(
    EC.presence_of_element_located((By.ID, "login-passwd"))
)
passwordElem.send_keys('password')
submitBtn=browser.find_element_by_id("login-signin")
submitBtn.click()

Let the buttons do their job just by clicking instead of submitting the whole form

Nimish Bansal
  • 1,719
  • 4
  • 20
  • 37