DEV Community

Cover image for How To Automate Your Browser using Selenium Webdriver and Python
Arvind Mehairjan
Arvind Mehairjan

Posted on

 

How To Automate Your Browser using Selenium Webdriver and Python

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.

Alt Text

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.

Importing modules

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 your-file-name.py.

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:

driver.get("https://www.python.org/")

Lastly, we want to close our browser. For that we need to enter this:

driver.close()

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:

driver.set_window_size(1080, 1080)

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")
enter_searchbar.send_keys("loops")

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']")
click_button.click()

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 .click().

Moving forwards and backwards

To move forwards in the browser there is only one line we need to write:

driver.forward()

The same applies to going backwards:

driver.back()

Conclusion

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.

Latest comments (7)

Collapse
 
labibllaca profile image
labibllaca

Hi Arvin,
congrats to your cool article. Do you know how to get value from js and than put that text to the webpage through the send_keys() ?

Here an example of mine:

self.driver = webdriver.Chrome()
usrname = self.driver.find_element_by_xpath('//*[@id="username"]')
loginUser = self.driver.execute_script("myName = prompt('Your name please: '); return {myName};")
usrname.send_keys(loginUser)
  • so first an prompt appears where you can give the login credentials and after that selenium works with that
  • but the prompt is only though javascript accesable (as far as I know) therefore my strange question :D

Cheers,
Labi

Collapse
 
arvindmehairjan profile image
Arvind Mehairjan • Edited

Good question.

To get text from a webpage you can use getText()

For example:
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 switchTo method. You can use the get_text_value in the prompt.
So something like this:

driver.switchTo().alert().sendKeys(get_text_value)

This is perhaps how you can solve it.

Hope it works for you! :)

Otherwise this doc can perhaps help you.

Collapse
 
labibllaca profile image
labibllaca

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.

Thanks again.

Thread Thread
 
arvindmehairjan profile image
Arvind Mehairjan • Edited

No problem mate. Good luck with your project.

Collapse
 
manishfoodtechs profile image
manish srivastava

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?

Collapse
 
devhammed profile image
Hammed Oyedele

You need to use xvfb to run headless browsers on your server.

You should read this blog post on how to use xvfb.

Collapse
 
manishfoodtechs profile image
manish srivastava

Thanks Hammed.... seems I can do it.

50 CLI Tools You Can't Live Without

The top 50 must-have CLI tools, including some scripts to help you automate the installation and updating of these tools on various systems/distros.