Search code examples
pythonseleniumyahoo-finance

How can I log in to Yahoo FInance using Python Selenium?


I've tried the following way, but every time I click on the "Next" button in the login, chromdriver would open a new tab and redirect me to this page.

from selenium import webdriver

path = "https://login.yahoo.com/config/login?.src=finance&.intl=us&.lang=en-US&.done=https%3A%2F%2Ffinance.yahoo.com%2Fquotes%2Flogin%2Fview%2Fv1%2F"
option = webdriver.Chrome()
option.add_argument("--incognito")
option.add_argument("--disable-notifications")
browser = webdriver.Chrome("/path/to/chromedriver", optiont=option)
browser.get(path)
browser.find_element_by_name("username").send_keys("username")

# all three attempts below redirected me to the page mentioned above
browser.find_element_by_name("signin").click()
browser.find_element_by_class_name("button-container").click()
browser.find_element_by_id("login-username-form").click()

I wonder this is some kind of secutiy they have considering the redirected page.

I also tried sending password in the hidden-input-container

browser.find_element_by_name("passwd").send_keys("password")

to get selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable error. I guess I need to press the Next button before sending the password.

I'd much appreciate any help on this issue.


Solution

  • Induce WebDriverWait() and wait for element_to_be_clickable()

    from selenium import webdriver
    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.chrome.options import Options
    
    path = "https://login.yahoo.com/config/login?.src=finance&.intl=us&.lang=en-US&.done=https%3A%2F%2Ffinance.yahoo.com%2Fquotes%2Flogin%2Fview%2Fv1%2F"
    option = Options()
    option.add_argument("--incognito")
    option.add_argument("--disable-notifications")
    browser = webdriver.Chrome(executable_path="/path/to/chromedriver",options=option)
    browser.get(path)
    WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.NAME,"username"))).send_keys("validusername")
    WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.NAME,"signin"))).click()
    WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.NAME,"password"))).send_keys("password")
    WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.ID,"login-signin"))).click()
    

    Browser snapshot:

    enter image description here