Fill Username and Password Using Selenium in Python

Fill username and password using selenium in python

Docs: https://selenium-python.readthedocs.io/navigating.html

For versions 4.3.0 (released in June 2022) and later, calls to find_element_by_* and find_elements_by_* were removed from Selenium. You need to use the new API:

from selenium.webdriver.common.by import By

driver = webdriver.Firefox(...) # Or Chrome(), or Ie(), or Opera()

# To catch <input type="text" id="passwd" />
password = driver.find_element(By.ID, "passwd")
# To catch <input type="text" name="passwd" />
password = driver.find_element(By.NAME, "passwd")

password.send_keys("Pa55worD")

driver.find_element(By.NAME, "submit").click()

The original response, for API versions 4.2.0 or previous:

driver = webdriver.Firefox(...)  # Or Chrome(), or Ie(), or Opera()

username = driver.find_element_by_id("username")
password = driver.find_element_by_id("password")

username.send_keys("YourUsername")
password.send_keys("Pa55worD")

driver.find_element_by_name("submit").click()

A note to your code: Select() is used to act on a Select Element (https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select).

How to fill username and password with selenium on a page where IDs are hidden?

You have picked the wrong ids from the page. The ones you have picked are type attribute and not ids.

You can use the below code to operate on the page(Have picked the correct ids) and have applied explicit wait on the first element:

from selenium import webdriver
import time
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options = webdriver.ChromeOptions()
options.add_argument('--ignore-ssl-errors=yes')
options.add_argument('--ignore-certificate-errors')
options.add_argument('--allow-running-insecure-content')
driver = webdriver.Chrome(options=options)

driver.get('https://120.72.92.102:10443/remote/login?lang=en')
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, "username")))

username = driver.find_element_by_id("username")
password = driver.find_element_by_id("credential")

username.send_keys("YourUsername")
password.send_keys("PassworD")

driver.find_element_by_id("login_button").click()

Fill username and password in input tag using selenium in python

The issue might be you are trying to find the element before it is visible.

You can wait for the element to be visible using the code below (I am also finding the ID by CLASS_NAME instead of XPATH, and capturing a screenshot to show the fields have been filled out)

driver.get("http://123.60.12.11:10016/#/login?url=http%3A%2F%2F123.60.12.11%3A10016%2F%23%2FMySociety%2FSocietyDetail")

wait = WebDriverWait(driver, 10)
username = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "username")))
username.send_keys("test@email.com")
pw = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "password")))
pw.send_keys("asdasd")

driver.get_screenshot_as_file("screenshot.png")

Working example

Python3 Selenium Fill Username and Password in Popup Dialog

Try to use following:

from selenium.webdriver.common.keys import Keys

browser = webdriver.Firefox('path_to_exe')
wait = WebDriverWait(browser, 10)
browser.get('https://target_website.com')
alert = wait.until(EC.alert_is_present())
alert.send_keys('username' + Keys.TAB + 'password')
alert.accept()

Note that this approach works in Firefox only

Selenium fill in user name/pwd, only filling one field

Instead of explicit localization of password element, it is often more practical something like this:

username.send_keys('bob' + Keys.TAB + 'pop' + Keys.ENTER) 


Related Topics



Leave a reply



Submit