Python Selenium Automate the Login Form - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
Here we will see how to automate the login form using selenium framework and python.
This example requires the following Softwares to be installed in your machine.
Install Python
Install Selenium
Install ChromeDriver and Browser
You can follow my previous article about how to install the selenium and kickstart with selenium HelloWorld example to get all prerequisites.
We are going to automate the Facebook login form using python.
from selenium import webdriver

driver = webdriver.Chrome(executable_path="D:\\softwares\\chromedriver_win\\chromedriver.exe")
driver.get("http://www.facebook.com")
driver.find_element_by_id("email").send_keys("facebookuser")
driver.find_element_by_id("pass").send_keys("password")
driver.find_element_by_id("u_0_b").click()
driver.close()

importing webdriver from selenium package
webdriver.Chrome() function is used to launch the web browser. for this, we have to provide the chromedriver.exe path as an executable_path=”{chromedriverpath}”
diver.get("URL") function loads a web page (http://www.facebook.com) in the current browser session.
driver.find_element_by_id() is a driver function used to finds an element in the web page and returns the element as WebElement. if the element wasn’t found it raises NoSuchElementException exception.
send_keys() is a WebElement function used to send simple key events or form fields. In our case, we are sending Facebook username and passwords to the form fields.
click() is a WebElement function is used to perform the click event on the element.
Finally closing the driver using the close() function.
On the above example, we used find_element_by_id(id) function, this function needs an exact dom element id to find it. So we have to provide that dom element id to this function to get the element.
Inspect the elements using developer tools (F12).
You can observe the above-highlighted dom elements. We can also use the name and class attributes for selection using find_element_by_name() and find_element_by_class_name() functions respectively, you can find more selectors here,
Output:
Finally, run the application so that you could see the browser window popping up and automate the login process from selenium on your behalf 🙂
C:\Users\Lenovo\python_samples>python selenium_helloworld.py

Selenium Helloworld Example
Selenium Locating Elements
Happy Learning 🙂

How install Python on Windows 10
How to upgrade Python PIP version on Windows
Python raw_input read input from keyboard
Python Selenium HelloWorld Example
Python Set Data Structure in Depth
Python – How to Read Google Search Results in Selenium
Python – Selenium Download a File in Headless Mode
Python Tuple Data Structure in Depth
Python – How to remove duplicate elements from List
Python List Data Structure In Depth
How to Remove Spaces from String in Python
Spring MVC Login Form Example Tutorials
Basic Android Login Form Example
Spring Boot Validation Login Form Example
Python – How to create Zip File in Python ?

How install Python on Windows 10
How to upgrade Python PIP version on Windows
Python raw_input read input from keyboard
Python Selenium HelloWorld Example
Python Set Data Structure in Depth
Python – How to Read Google Search Results in Selenium
Python – Selenium Download a File in Headless Mode
Python Tuple Data Structure in Depth
Python – How to remove duplicate elements from List
Python List Data Structure In Depth
How to Remove Spaces from String in Python
Spring MVC Login Form Example Tutorials
Basic Android Login Form Example
Spring Boot Validation Login Form Example
Python – How to create Zip File in Python ?