Overview
In this blog, we will see what is the use of Selenium Tool with Python language. In the current market, there are many tools available for web automation. Types of tools, Selenium, Cucumber, Ketalon Studio.
Benefits of Automation Testing Using Selenium:
- Easy to Install
- It works in Cross-Platform, Available in Mac, Windows, Linux & Solaris.
- Selenium Scrips run across various browsers like Chrome, Safari, Firefox, Opera
Installation
Let’s Get started with the Installation steps:
You need to install the below Things in your system.
- Python
- Pip Package management
- Pycharm Community “For code editing”
Use the following commands to install Selenium
pip install selenium
How to Use Selenium with different browsers
Now let’s check how it works:
- Open your text editor/ Pycharm and right some code by implementing the selenium library.
- Refer to the below Chrome driver code. It will launch the Google Chrome browser and Search Yudiz website.
from selenium import webdriver from webdriver_manager.chrome import ChromeDriverManager driver = webdriver.Chrome(ChromeDriverManager().install()) driver.get("https://www.yudiz.com/")
For Firefox Browser:
from selenium import webdriver driver = webdriver.Firefox() driver.get("https://www.yudiz.com/")
- Copy these few lines of code and paste it into your editor and save it as myCode.py
- Now Run it by python/python3 myCode.py
Once everything is fine it will open the new Chrome browser window and execute the Yudiz site URL.
Console Result:
You need to learn Some basic common web elements concepts for Web Automation.
How to find web elements for automation
- You can use id, Class, Xpath, Link Text to find web elements.
//TagName[@Attribute='Value']"
- // : Select current node.
- Tagname: Tagname of the particular node.
- @: Select attribute.
- Attribute: Attribute name of the node.
- Value: Value of the attribute.
Submit a simple web form using Selenium with python language
Refer to the Below code for Webform automation.
import time from selenium import webdriver from webdriver_manager.chrome import ChromeDriverManager web = webdriver.Chrome(ChromeDriverManager().install()) web.maximize_window() web.get("https://demoqa.com/automation-practice-form") def form(): time.sleep(3) web.find_element_by_id("firstName").send_keys("ABC") web.find_element_by_id("lastName").send_keys("XYZ") web.find_element_by_id("userEmail").send_keys("ABC@XYZ.com") web.find_element_by_xpath("//label[contains(text(),'Male')]").click() web.find_element_by_xpath("//input[@id='userNumber']").send_keys(1234567890) web.find_element_by_xpath("//label[contains(text(),'Reading')]").click() web.find_element_by_xpath("//textarea[@id='currentAddress']").send_keys("Browser Console") time.sleep(1) web.find_element_by_xpath( "//body/div[@id='app']/div[1]/div[1]/div[2]/div[2]/div[1]/form[1]/div[11]/div[1]/button[1]").click() form()
Console Log:
How it works: Refer to the attached video below
Conclusion
The advantage of Automation testing is to save the time and effort of repetitive tasks. Also,
It will help to ensure that everything is properly working using Automated methods. It helps to maintain product quality.