login to Yahoo using Python Selenium

心不动则不痛 提交于 2020-03-23 06:33:27

问题


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.


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/48145466/login-to-yahoo-using-python-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!