Search code examples
python-3.xselenium-webdriverselenium-chromedriver

Why does my Selenium script fail on password element?


I have this selenium script that can enter email address, hit the use password button but then fails on entering the password:

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

web = webdriver.Chrome()
web.implicitly_wait(5)
web.get('https://login.live.com/login.srf?wa=wsignin1.0&rpsnv=16&ct=1700481689&rver=7.0.6738.0&wp=MBI_SSL&wreply=https%3a%2f%2foutlook.live.com%2fowa%2f%3fcobrandid%3dab0455a0-8d03-46b9-b18b-df2f57b9e44c%26nlp%3d1%26deeplink%3dowa%252f%26RpsCsrfState%3de98b3d70-89d0-6e78-6a87-b7d5b1245a4e&id=292841&aadredir=1&CBCXT=out&lw=1&fl=dob%2cflname%2cwld&cobrandid=ab0455a0-8d03-46b9-b18b-df2f57b9e44c')

EmailSignIn = web.find_element(By.XPATH,"//*[@id='i0116']")
EmailSignIn.send_keys("[email protected]")

web.implicitly_wait(5)

EMailSignInContinueButton = web.find_element(By.XPATH, "//*[@id='idSIButton9']")
EMailSignInContinueButton.click()

web.implicitly_wait(5)

UsePasswordButton = web.find_element(By.XPATH, "//*[@id='idA_PWD_SwitchToPassword']")
UsePasswordButton.click()

web.implicitly_wait(5)

SelectEMailPasswordBox = web.find_element(By.XPATH,"//*[@id='i0118']")
SelectEMailPasswordBox.send_keys("FakePassword")

time.sleep(5)

Had no issues with the rest of the script so I am thinking there must be something funky about password boxes I don't know? Other online information seems outdated. Any help appreciated, thanks!

Tried to run script as above, fails on entering the password. Would like password to be populated in outlook.


Solution

  • Root cause of the issue: No need of the below line. I don't see anything in the HTML with attribute ID=idA_PWD_SwitchToPassword. (Unless I am missing something, or there is additional screen/button displaying to you but not to me on my system)

    UsePasswordButton = web.find_element(By.XPATH, "//*[@id='idA_PWD_SwitchToPassword']")
    UsePasswordButton.click()
    

    Check the below working refactored code:

    from selenium import webdriver
    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
    
    web = webdriver.Chrome()
    web.maximize_window()
    web.get('https://login.live.com/login.srf?wa=wsignin1.0&rpsnv=16&ct=1700481689&rver=7.0.6738.0&wp=MBI_SSL&wreply=https%3a%2f%2foutlook.live.com%2fowa%2f%3fcobrandid%3dab0455a0-8d03-46b9-b18b-df2f57b9e44c%26nlp%3d1%26deeplink%3dowa%252f%26RpsCsrfState%3de98b3d70-89d0-6e78-6a87-b7d5b1245a4e&id=292841&aadredir=1&CBCXT=out&lw=1&fl=dob%2cflname%2cwld&cobrandid=ab0455a0-8d03-46b9-b18b-df2f57b9e44c')
    wait = WebDriverWait(web,15)
    
    EmailSignIn = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@id='i0116']")))
    EmailSignIn.send_keys("[email protected]")
    
    EMailSignInContinueButton = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@id='idSIButton9']")))
    EMailSignInContinueButton.click()
    
    SelectEMailPasswordBox = wait.until(EC.element_to_be_clickable((By.XPATH,"//*[@id='i0118']")))
    SelectEMailPasswordBox.send_keys("FakePassword")
    
    time.sleep(20)
    

    Result:

    enter image description here