99

How can I auto fill the username and password over the link below:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

chromedriver = 'C:\\chromedriver.exe'
browser = webdriver.Chrome(chromedriver)
browser.get('http://www.example.com')

After that I really do not know:

username = Select(browser.find_element_by_name('Username'))
password = Select(browser.find_element_by_name('Password'))
username.select_by_visible_text("text")
password.select_by_visible_text("text")

8 Answers 8

145

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).

Sign up to request clarification or add additional context in comments.

9 Comments

Hey, do you know how to read the contents of the page that's loaded after submitting the form? I am writing a test for a website, and I managed to submit the form, and now I am lost. In other words, I want to get the contents of the restricted, members only landing page.
@NinoŠkopac The "selenium" object works like a browser. Once you "click" it loads the new landing page, so you can do 'selenium.find_element_by_id("whatever")'. Read also about "selenium.implicitly_wait()" to wait between page loads.
Note, in the above answer, selenium should be the driver handle obtained, for example, selenium = webdriver.Firefox()
This answer is outdated after the 2022 API changes. See answer above by @haritha_kh for the corrections ( stackoverflow.com/questions/21186327/… )
@RichLysakowskiPhD you can edit answers if they are wrong/outdated.
|
32

Use WebElement.send_keys method to simulate key typing.

name in the code (Username, Password) does not match actual name of the elements (username, password).


username = browser.find_element_by_name('username')
username.send_keys('user1')

password = browser.find_element_by_name('password')
password.send_keys('secret')

form = browser.find_element_by_id('loginForm')
form.submit()

# OR  browser.find_element_by_id('submit').click()

3 Comments

When I try this I get this error : 'StatefulBrowser' object has no attribute 'find_element_by_name'
@AnaClaudia, it is possible api changed since I posted this answer. Please check documentation.
This answer is outdated after the 2022 API changes. See answer above by @haritha_kh for the corrections ( stackoverflow.com/questions/21186327/… )
11
user = driver.find_element_by_name("username")
password = driver.find_element_by_name("password")
user.clear()
user.send_keys("your_user_name")
password.clear()
password.send_keys("your_password")
driver.find_element_by_name("submit").click()

Note:

  • we useuser.clear() in order to clear the input field.
  • for locating submit button you can use any other method based on the page source code. for info see locating elements

1 Comment

This answer is outdated after the 2022 API changes. See answer above by @haritha_kh for the corrections ( stackoverflow.com/questions/21186327/… )
7

In some cases when the element is not interactable, sendKeys() doesn't work and you're likely to encounter an ElementNotInteractableException.

In such cases, you can opt to execute javascript that sets the values and then can post back.

Example:

url = 'https://www.your_url.com/'

driver = Chrome(executable_path="./chromedriver")
driver.get(url)

username = 'your_username'
password = 'your_password'

#Setting the value of email input field
driver.execute_script(f'var element = document.getElementById("email"); element.value = "{username}";')

#Setting the value of password input field
driver.execute_script(f'var element = document.getElementById("password"); element.value = "{password}";')

#Submitting the form or click the login button also
driver.execute_script(f'document.getElementsByClassName("login_form")[0].submit();')

print(driver.page_source)

Reference:

https://www.quora.com/How-do-I-resolve-the-ElementNotInteractableException-in-Selenium-WebDriver

Comments

2

Here is the complete answer.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

chrome_driver_path = 'C:\\chromedriver.exe'
browser = webdriver.Chrome(service=Service(chrome_driver_path))
browser.get('http://www.example.com')

username = browser.find_element(By.NAME, 'Username')
password = browser.find_element(By.NAME, 'Password')

username.send_keys("yourUsername") #type your own username here
password.send_keys("yourPassword") #type your own password here

browser.find_element(By.NAME, 'submit').click()

Since find_element_by_name() is deprecated, you can use find_element(By.NAME, 'name').

Also you have to import from selenium.webdriver.common.by import By

1 Comment

This is a great answer in October 2022. The find_element() function syntax changed. This answer works pretty much "as is". Thank you !!
1

For this purpose, you can use the command

driver.find_element_by_xpath()

For that, right-click on a button/field you interest (login,submit, username, password etc). After that In Chrome you choose [Inspect-> copy-> copy full xPath], while in in Firefox it be [Inspect-> copy-> XPath]. Now you do the following:

login_knob = driver.find_element_by_xpath("here you put code you copied for the push button").click()
username_field = driver.find_element_by_xpath("here you put code you copied for the field username").send_keys('username')
password_field = driver.find_element_by_xpath("here you put code you copied for the field password").send_keys('password')
login_knob2 = driver.find_element_by_xpath("here you put code you copied for the login button").click()

Example of a copied code

/html/body/div[1]/div[2]/div[1]/div/div[1]/div/div[1]/div/div[2]/div[2]/button[1]

Comments

0
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait

# If you want to open Chrome
driver = webdriver.Chrome()
# If you want to open Firefox
driver = webdriver.Firefox()

username = driver.find_element_by_id("username")
password = driver.find_element_by_id("password")
username.send_keys("YourUsername")
password.send_keys("YourPassword")
driver.find_element_by_id("submit_btn").click()

2 Comments

It worked to me, but I needed to use the "find_element_by_class" instead. And also add the Chrome driver path.
This answer is outdated after the 2022 API changes. See answer above by @haritha_kh for the corrections ( stackoverflow.com/questions/21186327/… )
-1

I am new to selenium and I tried all solutions above but they don't work. Finally, I tried this manually by

driver = webdriver.Firefox()
import time

driver.get(url)

time.sleep(20)

print (driver.page_source.encode("utf-8"))

Then I could get contents from web.

2 Comments

solution is different as compared to question
If you need to wait for the page to load (a slow page response or connection), you should use selenium-python.readthedocs.io/… . Your code waits for 20 seconds always. With implicitly_wait, if the page loads quickly you don't have to wait the whole 20 seconds. But is a different problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.