In this Python Selenium tutorial we want to learn about Python Selenium WebDriver Tutorial, In today’s world automation has become an important part of different tasks, including web browsing and testing. Python is one of the popular programming language, and it offers several libraries and tools to facilitate automation, and one of that tool is Selenium WebDriver, which allows developers to control web browsers programmatically.
Python Selenium WebDriver Tutorial
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, you can download the driver, and you can add the EXE in your working directory where you Python file is located.
1 |
pip install selenium |
Note: You can download the drivers from here.
First of all we need to import required modules from selenium library.
1 2 |
from selenium import webdriver from selenium.webdriver.common.keys import Keys |
After that we need to initialize the WebDriver, which will control the browser. In this example, we will use Google Chrome as our browser. If you prefer a different browser, make sure to download the respective WebDriver executable and set the appropriate path.
1 |
driver = webdriver.Chrome() |
To automate a Google search, we need to open the Google homepage. Use the get() method of the WebDriver to navigate to the desired URL:
1 |
driver.get("https://www.google.com") |
In order to enter our search query, we need to locate the search input element on the Google homepage. Inspect the element using your browser developer tools to find the appropriate selector. For the Google search input, we can use the name attribute, which is typically consistent across different browser versions:
1 |
search_input = driver.find_element("name", "q") |
Now that we have identified the search input element, we can enter our desired search query. Use the send_keys() method to type the text into the input field:
1 |
search_input.send_keys("Python Selenium WebDriver tutorial") |
For performing the search, we can use the send_keys() method with the Keys.ENTER argument. This simulates pressing the Enter key after entering the search query:
1 |
search_input.send_keys(Keys.ENTER) |
After that the search is performed, we can extract and process the search results using different techniques. For simplicity, let’s print the titles of the search results:
1 2 3 |
search_results = driver.find_elements("css selector", "h3") for result in search_results: print(result.text) |
After we have obtained the desired information, it’s good practice to close the browser to free up system resources:
1 |
driver.quit() |
This is the complete code for this article
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 |
from selenium import webdriver from selenium.webdriver.common.keys import Keys # Initialize the WebDriver (Chrome in this case) driver = webdriver.Chrome() # Open Google.com driver.get("https://www.google.com") # Find the search input element search_input = driver.find_element("name", "q") # Enter the search query search_input.send_keys("Python Selenium WebDriver tutorial") # Submit the search search_input.send_keys(Keys.ENTER) # Extract and print the search results' titles search_results = driver.find_elements("css selector", "h3") for result in search_results: print(result.text) # Close the browser driver.quit() |
These will be the printed search titles using Selenium Web Driver
Learn More on Python Selenium
Subscribe and Get Free Video Courses & Articles in your Email