Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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") 
like image 384
2964502 Avatar asked Jan 17 '14 12:01

2964502


People also ask

How do I use username and password in alert box using Selenium Python?

alert(); //Selenium-WebDriver Java Code for entering Username & Password as below: driver. findElement(By.id("userID")). sendKeys("userName"); driver. findElement(By.id("password")).

How does Selenium pass username and password in Alert?

To handle the basic authentication popup, we can pass the username and password along with the web page's URL. When the login pop-up is prompted, we enter the username as “admin” and the password as “admin” and then login. Thus, the user would be successfully logged into the website.

How do I bypass password in Selenium?

Passing username and password in the URL helps to avoid the login prompt. This is achieved by encoding the username and password in the URL, that is, prepending username:password@ to the hostname in the URL.


2 Answers

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'): Username capitalized doesn't match anything.
  • Select() is used to act on a Select Element (https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select)
like image 102
xbello Avatar answered Oct 14 '22 21:10

xbello


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() 
like image 33
falsetru Avatar answered Oct 14 '22 21:10

falsetru