Login To Yahoo Using Python Selenium
Solution 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 Byfrom 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 Byfrom 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
Solution 2:
from selenium import webdriver
from selenium.webdriver.common.by import Byfrom 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
Post a Comment for "Login To Yahoo Using Python Selenium"