Python code for Yahoo login

7596

Table of Contents

Python code for Yahoo mail login

To implement this code we will need to import webdriver from the Selenium library, for Python. We will set-up a Firefox driver using Selenium. We need to access the login URL page for Yahoo mail. After that, we need to access XPATH from Yahoo login page source such as: username, password. Finally, we will send a post request by clicking the login button or sending Return/Enter key using Selenium Keys. It is one of the simplest ways to login without using API. Instead of using the requests library, we chose to use Selenium because it makes a job lot easier, as we do not need to set additional parameters.

Python code implementation for Yahoo mail login using Selenium.

 

Code:

 

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from time import sleep

options = Options()
driver = webdriver.Firefox(options=options, executable_path=r'C:\driver\geckodriver.exe')
wait = WebDriverWait(driver, 10)
driver.get('https://login.yahoo.com')
driver.maximize_window()
sleep(5)
driver.find_element_by_xpath('//*[@id="login-username"]').send_keys('username')
sleep(5)
driver.find_element_by_xpath('//*[@id="login-username"]').send_keys(Keys.RETURN)
sleep(5)
driver.find_element_by_xpath('//*[@id="login-passwd"]').send_keys('password')
sleep(5)
wait.until(EC.presence_of_element_located((By.ID, 'login-passwd'))).send_keys(Keys.RETURN)

 

Steps required for Yahoo mail login using Python

  • Setting up Firefox driver using Selenium
  • Carefully set the Fullpath name, where the geckodriver.exe file is placed.
  • The login page URL (such as https://login.yahoo.com) – GET Request operation
  •  XPATH for username – Send_Keys (it can also be CSS selector, class name or tag instead of XPATH):  //*[@id=”login-username”]
  • XPATH for pressing Enter key using Selenium: //*[@id=”login-username”]
  • XPATH for the password – Send_Keys operation : //*[@id=”login-passwd”]
  • Locate element by ID to press return or Enter Key using Selenium and sending a post request : login-passwd