97

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")
2964502
  • 4,301
  • 12
  • 35
  • 55

8 Answers8

144

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

xbello
  • 7,223
  • 3
  • 28
  • 41
  • 3
    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. – The Onin Jan 31 '17 at 18:49
  • 3
    @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. – xbello Feb 01 '17 at 11:03
  • 2
    Note, in the above answer, selenium should be the driver handle obtained, for example, selenium = webdriver.Firefox() – Yu Shen Mar 28 '18 at 18:51
  • @xbello Thank you very much! – Sohan Das Oct 15 '18 at 10:52
  • 1
    This answer is outdated after the 2022 API changes. See answer above by @haritha_kh for the corrections ( https://stackoverflow.com/questions/21186327/fill-username-and-password-using-selenium-in-python/71814183#71814183 ) – Rich Lysakowski PhD Oct 14 '22 at 07:04
  • @RichLysakowskiPhD you can edit answers if they are wrong/outdated. – xbello Oct 15 '22 at 10:37
  • @xbello, do you know why sometimes the ID of the html element for the password textbox is sometimes "password" and sometimes it would be "Passwd", the reason my login automation fails on this part: <> – toking Apr 26 '23 at 02:43
  • @toking it's up the web developer. It could be "password", "passwd", a random string, or even have no name nor id attributes. You have to view the page source code and find what you want. Usually forms are generated by frameworks, and they default to name="password" or id="password". – xbello Apr 26 '23 at 06:54
  • @xbello thank you very much for your brilliant reply. Unfortunately { driver.find_element(By.NAME, "submit").click() } on my desired url returns the following error message elenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [name="submit"]. How should I parse the html page to understand what fields the login page requires. – Andreuccio May 20 '23 at 11:44
  • @Andreuccio, manually you have to right-click on the button that does the submit, and click "inspect". A window opens, where you have to find the button you want to "click" and figure out a way to get it from the DOM (by Name, by Id, or by Css Selector / XPath as shown [here](https://github.com/xbello/wagtail-tag-manager.git) ) – xbello May 21 '23 at 15:07
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()
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • When I try this I get this error : 'StatefulBrowser' object has no attribute 'find_element_by_name' – Ana Claudia Nov 05 '19 at 01:11
  • 2
    @AnaClaudia, it is possible api changed since I posted this answer. Please check documentation. – falsetru Nov 05 '19 at 04:19
  • This answer is outdated after the 2022 API changes. See answer above by @haritha_kh for the corrections ( https://stackoverflow.com/questions/21186327/fill-username-and-password-using-selenium-in-python/71814183#71814183 ) – Rich Lysakowski PhD Oct 14 '22 at 07:04
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
Anant Singh
  • 245
  • 3
  • 6
  • This answer is outdated after the 2022 API changes. See answer above by @haritha_kh for the corrections ( https://stackoverflow.com/questions/21186327/fill-username-and-password-using-selenium-in-python/71814183#71814183 ) – Rich Lysakowski PhD Oct 14 '22 at 07:04
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

Rithin Chalumuri
  • 1,739
  • 7
  • 19
1

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

haritha_kh
  • 21
  • 1
  • 1
  • 4
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]

yujanosha
  • 9
  • 5
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()
Plabon Dutta
  • 6,819
  • 3
  • 29
  • 33
  • It worked to me, but I needed to use the "find_element_by_class" instead. And also add the Chrome driver path. – Davidson Lima Jan 26 '22 at 23:26
  • This answer is outdated after the 2022 API changes. See answer above by @haritha_kh for the corrections ( https://stackoverflow.com/questions/21186327/fill-username-and-password-using-selenium-in-python/71814183#71814183 ) – Rich Lysakowski PhD Oct 14 '22 at 07:03
-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.

  • solution is different as compared to question – ravishankar chavare Jun 28 '21 at 17:12
  • If you need to wait for the page to load (a slow page response or connection), you should use https://selenium-python.readthedocs.io/api.html#selenium.webdriver.remote.webdriver.WebDriver.implicitly_wait . 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. – xbello Oct 15 '22 at 10:33