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.
Getting started and Installation
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
Open, navigating and closing to the browser
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:
Changing Windows Size
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).
Entering text in the search bar
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.
Clicking on buttons
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
Moving forwards and backwards
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.
Oldest comments (7)
Hello Arvind... nice article. I was able to use it on local computer but how I can use it on server.... headless chrome etc stuff?
You need to use
xvfbto run headless browsers on your server.
You should read this blog post on how to use xvfb.
Thanks Hammed.... seems I can do it.
congrats to your cool article. Do you know how to get value from js and than put that text to the webpage through the
Here an example of mine:
To get text from a webpage you can use
get_text_value = driver.find_element_by_xpath("your-xpath").getText()
With the value you found you can switch to the prompt message.
You can do that by using the
switchTomethod. You can use the
get_text_valuein the prompt.
So something like this:
This is perhaps how you can solve it.
Hope it works for you! :)
Otherwise this doc can perhaps help you.
Oh wow thanks Arra, it looks very promising. I'll definitly going to try it. And the doc is just perfect for what I need to learn.
My question was kind of semi-automatation of an proces I'd like to create. For example the download of an specific pdf in a webpage that needs authentification. I looks like the pdf that this magazine-webpage provides comes every month and the xpath is changing by just an character (the one othe month of a year). By getting the desired number from an user that s/he puts i an prompt this character would be saved in the script and proceded with it.
No problem mate. Good luck with your project.