'Fill username and password using selenium in python
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")
Solution 1:[1]
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()
Notes to your code:
find_element_by_name('Username'):Usernamecapitalized doesn't match anything.Select()is used to act on a Select Element (https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select)
Solution 2:[2]
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()
Solution 3:[3]
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 use
user.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
Solution 4:[4]
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
Solution 5:[5]
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()
Solution 6:[6]
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
Solution 7:[7]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 | Anant Singh |
| Solution 4 | Rithin Chalumuri |
| Solution 5 | Plabon Dutta |
| Solution 6 | haritha_kh |
| Solution 7 | user10070370 |
