Are you interested in learning in improving your automation process or do you just want to spend less time doing repetitive tasks? Here is a short and basic tutorial how you can use Selenium Webdriver and Python to automate your browser.
First we need to install Python. Head to the Python website and download & install the Python software on your computer.
After you have installed Python you need to install Selenium. To install Selenium you need enter the following command in your terminal or command prompt:
pip install selenium
In this tutorial we are going to use Chrome browser to automate your browser. For that we have to install Chromedriver. Head to the Chromedriver website and download the Chromedriver. Make sure the version of Chromedriver is the same as the browser you are using. To check your browser version go to Help settings of your browser and check the version you are using. The Chromedriver allows us to interact with the browser and navigate through it.
Open your text editor and make a new file. Save it as a Python file with name such as
your-file-name.py. For the
chromedriver.exe you have downloaded, make sure it is in the same directory as
Next, you need to import the webdriver from the Selenium module. On the top of your page enter the following code below:
from selenium import webdriver
First we need to open the Chrome browser. For this we need to use the Chromedriver. Enter the following code:
driver = webdriver.Chrome("chromedriver.exe")
After opening the browser we need to navigate to a specific URL. In this example we are heading to the Python website. For navigating to Python website we need to enter the following code:
Lastly, we want to close our browser. For that we need to enter this:
Selenium gives us the ability to change the browser window size. To set the windows size we can use the following code:
Here you can change the width and height in pixels (in the above example we set the width and height of our browser for 1080px).
Next, we want to enter text in the search bar on top of the Python website. We need to find the element first of the searchbar. There are several way to locate elements in Selenium. In this tutorial we will locate the ID of the search field. If you inspect the element of the search field, the id of it is
id-search-field. We can enter the following code to enter text in the search bar:
enter_searchbar = driver.find_element_by_id("id-search-field")
In the above code we created a variable called
enter_searchbar where we identify how we locate the element. In the second line we use the
send_keys to enter the term "loops" in the search bar.
After we have entered our term we want to display the results. To display the results we have to click on the "GO" button. We have to find this button element and use it in our code. You can enter the following code to get this result.
click_button = driver.find_element_by_xpath("//button[normalize-space()='GO']")
We made a variable called
click_button. Here we identify how the element will be found. In this example we find the element by using XPath. XPath is a technique that allows users to interact with the HTML page. It enables to navigate through the XML structure and use different attributes to navigate through.
In this second line we use the variable again and are going to click on using the built-in
To move forwards in the browser there is only one line we need to write:
The same applies to going backwards:
This is the bare basics you need to know to automate your browser processes. There are more actions you can do automate. For more tips and tricks, check out my YouTube video below where I share more Selenium and Python tips to automate your browser:
You can find the full source code here.