Search code examples
pythonselenium-webdriverstock

Selenium (python) issues with Schwab, can't login to Schwab


I've tried multiple ways of trying to login, but I have no idea why it's not working. I've tried xpath, ccs, and by Id, but nothing works. I'm fairly new to selenium, so I could be missing something important.

I tried using other people's python codes in selenium, but they were outdated, and I tried to fix them, and they still didn't work. I wonder if selenium works for Schwab at all.

(I changed the username and password inputs, obviously, but still wouldn't input anything)

Code:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
import time


LOGIN_URL = 'https://www.schwab.com/public/schwab/nn/login/mobile-login.html&lang=en'
username = "Username"
password = "Password"

options = Options()
options.add_experimental_option("detach", True)

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)

time.sleep(3)

driver.get(LOGIN_URL)
#driver.switch_to.frame('mobile-login')
username = driver.find_element('LoginId')
password = driver.find_element('Password')
username.send_keys(username)
password.send_keys(password)
driver.find_element('RememberLoginId').click()
driver.find_element('Submit').click()











#username_entry = driver.find_element(By.XPATH, '//*[@id="LoginId"]')
#username_entry.send_keys(username)

#password_entry = driver.find_element(By.XPATH, '//*[@id="Password"]')
#password_entry.send_keys(password)

#login_button = driver.find_element(By.XPATH, '//*[@id="loginSubmitBtn"]')
#login_button.click()

time.sleep(5)
print(driver.current_url)

driver.quit()


Solution

  • You first have to switch to the iframe element then driver.switch_to.frame(driver.find_element(By.ID, 'lmsSecondaryLogin'))

    Also the locators were incorrect

    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.chrome.options import Options
    from webdriver_manager.chrome import ChromeDriverManager
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    import time
    
    LOGIN_URL = 'https://www.schwab.com/public/schwab/nn/login/mobile-login.html&lang=en'
    username = "Username"
    password = "Password"
    
    options = Options()
    options.add_experimental_option("detach", True)
    
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
    
    time.sleep(3)
    
    driver.get(LOGIN_URL)
    driver.switch_to.frame(driver.find_element(By.ID, 'lmsSecondaryLogin'))
    userName = driver.find_element(By.ID, 'loginIdInput')
    passWord = driver.find_element(By.ID, 'passwordInput')
    userName.send_keys("username")
    passWord.send_keys("password")
    driver.find_element(By.ID, 'remember-me-checkbox-stack').click()
    input("Enter any key")
    
    # Wait until the element is clickable
    wait = WebDriverWait(driver, 10)
    element = wait.until(EC.element_to_be_clickable((By.ID, 'btnLogin')))
    element.click()
    
    
    print(driver.current_url)
    

    Manually Click Login

    # Manually Click login
    input("Enter any key")