Selenium WebDriver Web Automation Using Python
What is Selenium?
Selenium is a free and open-source automation testing toolkit for web applications running on a variety of platforms and browsers. Using the Selenium framework, you may interact with HTML components on any website. Instead of only gathering data from a webpage, Selenium allows us to interact with the elements.
What is Python?
Python is a server-side web development, software development, mathematics, and system scripting programming language.
It is also well-liked for rapid application development and as a scripting or glue language to connect existing components due to its high-level, built-in data structures, dynamic typing, and dynamic binding. Python’s simple-to-learn syntax and emphasis on readability lower the expenses associated with program maintenance. The ability to use modules and packages in Python also makes it easier to reuse code and create modular programs. Python is an open-source community language, therefore a huge number of independent programmers are constantly generating libraries and features for it.
Using Python with Selenium WebDriver: Why?
- Web, UI, and mobile app automation are simple.
- Test web services and REST APIs.
- A simple transition to Behaviour Driven Development (BDD) or Test Driven -Development (TDD) approach to testing, among many other characteristics.
- Compared to other programming languages, runs faster.
- Today’s industry uses Python extensively to create tools, applications, and APIs because it is a high-level language.
Visit the Python website to download and install Python
- https://www.python.org/downloads
How to Python Steps?
- Step 1: Download the most recent Python version
- Step 2: Install Python To install Python, launch the.exe file after the download is complete. Click Install Now to continue.
- Step 3: Python Installed Python is currently being installed
- Step 4: When it’s finished, a screen stating that the Setup was successful will appear. Select “Close” at this point.
How to set up Selenium WebDriver?
We need an IDE to launch projects before installing Selenium WebDriver. Pycharm IDE is undoubtedly the best IDE tool for Python created by JetBrains.
It is accessible at https://www.jetbrains.com/pycharm for download.
Select “Download Now” to start the Community Edition download.
Choose a browser: Writing test scripts for all the major browsers, including Mozilla Firefox, Google Chrome, Internet Explorer, Opera, Safari, and headless browsers, is achievable using Selenium automation. Visit this website https://www.selenium.dev/downloads to see the browsers that Selenium supports.
Now, complete the steps below to run the scripts with, say, Google Chrome:
- Install ChromeDriver server.
- Save the files you’ve extracted from chromedriver_win32.zip to your local computer.
- Use the ChromeDriver server’s path in the test script.
- Start the script
Good to Read:-06 Easy Steps to Setup Selenium Python Pytest
How Does Internal Selenium Automation Operate?
In order for Selenium WebDriver to function, orders are sent to the browser drivers through the WebDriver API, and the browsers themselves then carry out those commands. By use of the browser-specific APIs that the browser vendors have made available, the browser drivers interact with the web browser.
Syntax of Locators Elements in Selenium Python
driver.find_element_by_id() driver,find_element_by_name() driver.find_element_by_xpath() driver.find_element_by_class_name() driver.find_element_by_tag_name() driver.find_element_by_css_selector() driver.find_element_by_link_text() driver.find_element_by_partial_link_text() For find multiple elements , we use find_element_*
Python Script for Selenium-Open Google Page
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.keys import Keys from webdriver_manager.chrome import ChromeDriverManager import time driver = webdriver.Chrome() driver.get("https://www.google.com/") time.sleep(2) driver.close() driver.quit() print("test Completed")
When we run this script, the Chrome browser will open with Google.com.
Good to Read:- How to Invoke Chrome Browser and Load Website to Automate?
Python Script for Selenium – Login Into Facebook
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from webdriver_manager.chrome import ChromeDriverManager import time driver = webdriver.Chrome() user_name = 'User email id' password = 'User password' driver.get("https://www.facebook.com") Search_box = driver.find_element(By.ID, 'email') Search_box.send_keys(user_name) Search_box = driver.find_element(By.ID ,'pass') Search_box.send_keys(password) time.sleep(3) driver.close()
When we run this script, The Facebook login page will open.
Consequence
Python is the ideal choice for Selenium testing due to its ease of use. Most organizations choose the former because of its straightforward programming syntax. Unlike other programming languages, Python makes it easier to develop Selenium scripts. PyTest, a Python Selenium framework, is the ideal option for complicated functional tests.