RIPPLR
RIPPLR

Reputation: 316

Cannot type Password via SendKeys() in Selenium Python

I am trying to write a program using Selenium WebScraping to interact with my email. The code I pasted below is just for the login process. I changed the email and password.

from selenium import webdriver

#Edge
driver = webdriver.Chrome()

#Open the website
driver.get('https://login.live.com/login.srf?wa=wsignin1.0&rpsnv=13&ct=1602431674&rver=7.0.6737.0&wp=MBI_SSL&wreply=https%3a%2f%2foutlook.live.com%2fowa%2f%3fnlp%3d1%26RpsCsrfState%3d2c3ee82f-9893-34e5-fd04-e3b4c3e2ccc4&id=292841&aadredir=1&CBCXT=out&lw=1&fl=dob%2cflname%2cwld&cobrandid=90015')

log_in_button = driver.find_element_by_name("loginfmt")

log_in_button.send_keys('[email protected]')

next_button = driver.find_element_by_id("idSIButton9")

next_button.click()

password_button = driver.find_element_by_name("passwd")

password_button.send_keys('my_password')

When I run this however (with the right email and password), the program cannot seem to enter the password at the last step. There is no error log, and my program enters my email just fine, but it cannot enter the password. I tried using several different ways to locate the HTML element including

find_element_by_type()

find_element_by_id()

find_element_by_class_name()

and also

find_element_by_css_selector()

Most of these methods ended up with StaleElementReferenceException, some ended up with Cannot Locate Element, and the rest didn't work at all.

Does anyone know a method to typing in a password via Selenium, I am assuming that either I am using the wrong element, or Microsoft purposely blocks this kind of behavior for some reason.

Any help is greatlly appreciated. Thanks.

Upvotes: 0

Views: 617

Answers (1)

KunduK
KunduK

Reputation: 33384

The best practice is to avoid time.sleep() wherever possible. time.sleep() unnecessary slow down your scrpits.

Best practice says always use Explicit wait to avoid synchronization issue.

Use WebDriverWait() and wait for element_to_be_clickable()

password_button =WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.NAME,"passwd")))

You need to import below libraries.

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

Further learning you can refer below Explicit Waits

Upvotes: 1

Related Questions