[Python] I am not able to login using selenium to www.moneycontrol.com

Discussão em 'Python' iniciado por Stack, Fevereiro 3, 2025.

  1. Stack

    Stack Membro Participativo

    I tried below code to fill the login form into www.moneycontrol.com but it seems little bit complicated. Login form is multi-layered,

    1. hover or click "Hello, Login". click Log-in button inside floating form
    2. Login with password tab
    3. then fill email and password to enable login button.

    Help is appreciated, code is

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    import time
    import pandas as pd
    from io import StringIO
    from bs4 import BeautifulSoup

    chrome_options = Options()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-dev-shm-usage')

    driver = webdriver.Chrome(options=chrome_options)
    driver.get('https://www.moneycontrol.com')
    email = "xxxxxxx@gmail.com"
    password = "Qxxxxxxx"
    # Click on the "Login" button
    print(driver.page_source)
    login_button = driver.find_element(By.XPATH, '//a[contains(text(), "Hello, Login")]')
    login_button.click()
    time.sleep(3) # Wait for login popup to appear
    # Click on the "Login" button
    login_button = driver.find_element(By.XPATH, '//a[contains(text(), "Log-in")]')
    login_button.click()
    time.sleep(3) # Wait for login popup to appear
    # Click on the "Login" button
    login_button = driver.find_element(By.XPATH, '//a[contains(text(), "Login with Password")]')
    login_button.click()
    time.sleep(3) # Wait for login popup to appear
    # Switch to login iframe if present
    try:
    iframe = driver.find_element(By.TAG_NAME, "iframe")
    driver.switch_to.frame(iframe)
    time.sleep(2)
    except:
    pass # If no iframe, continue normally

    # Enter email/username
    email_field = driver.find_element(By.CSS_SELECTOR, '#mc_login > form > div:nth-child(1) > div > input[type=text]')
    email_field.send_keys(email)

    # Enter password
    password_field = driver.find_element(By.CSS_SELECTOR, '#mc_login > form > div:nth-child(2) > div > input[type=password]')
    password_field.send_keys(password)

    # Click on Login button
    submit_button = driver.find_element(By.CSS_SELECTOR, '#mc_login button[type="submit"]')
    submit_button.click()

    # Wait for login to process
    time. Sleep(5)

    Continue reading...

Compartilhe esta Página