In this article we want to learn about Python Selenium Basics, so Selenium is a set of tools for browser automation, and it is mostly used for testing applications. but the usage of selenium is not limited to testing of applications, but it can also be used for screen scrapping and repetitive tasks automation in a browser. so selenium supports automation for different browsers like firefox, internet explorer, google chrome, safari and opera.
Python Selenium Basics
For working with the example of this tutorial you need some requirements, first you should have installed Python in your system, then we need to install Python Selenium and you can use pip for that like this, also you need to driver for specific browser.
1 |
pip install selenium |
Note: You can download the drivers from here.
To begin with Selenium, we first need to import the necessary modules and create an instance of a web driver. Below is an example of launching the Chrome browser:
1 2 3 4 |
from selenium import webdriver # Create a new instance of the Chrome driver driver = webdriver.Chrome() |
After launching the browser, we can instruct Selenium to navigate to a specific URL. Let’s use the https://demoqa.com/login page as an example:
1 2 |
# Navigate to the login page driver.get("https://demoqa.com/login") |
Selenium provides different methods to interact with web elements such as buttons, input fields and dropdowns. We can locate elements using different techniques, including XPath, CSS selectors and element IDs. This is an example of logging in by filling in the username and password fields:
1 2 3 4 5 6 7 8 9 10 11 12 |
from selenium.webdriver.common.by import By # Find the username and password fields username_field = driver.find_element(By.ID, "userName") password_field = driver.find_element(By.ID, "password") # Enter the login credentials username_field.send_keys("geekscoders") password_field.send_keys("Geekscoders@123") # Submit the form driver.find_element(By.ID, "login").click() |
Web pages may load at different speeds, and elements may appear dynamically. To ensure that our automation scripts work reliably, we can incorporate wait times to synchronize with the page loading process. This is an example of using an explicit wait to wait for an element to become clickable:
1 2 3 4 5 |
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # Wait until the element is clickable element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "someElementID"))) |
This is the complete code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # Create a new instance of the Chrome driver driver = webdriver.Chrome() # Navigate to the login page driver.get("https://demoqa.com/login") # Find the username and password fields username_field = driver.find_element(By.ID, "userName") password_field = driver.find_element(By.ID, "password") # Enter the login credentials username_field.send_keys("your_username") password_field.send_keys("your_password") # Submit the form driver.find_element(By.ID, "login").click() # Wait until the welcome message appears welcome_message = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, "welcome-message"))) # Print the welcome message print(welcome_message.text) # Close the browser driver.quit() |