Selenium basics to automate whatever you want

0 16
Avatar for jesgorbal
2 years ago

From time to time I like to learn how to use some of the Python libraries, because of the wide variety of things you can do.
One of the things I do in my job is software testing. Looking for tools to automate this kind of tasks I came across the Selenium library. Although it can be used for testing, it also allows you to automate any task you can do with a browser.

Things we need to install

Whenever we are going to use a tool we need to install a number of things.

For this case we will need to have Python installed. In my case I have version 3.6.

Like other Python libraries, we can install Selenium using pip install: pip install selenium

Finally, we will need to download a webdriver to control the browser. In my case, using the Chrome browser, I have installed the webdriver from the website: https://chromedriver.storage.googleapis.com/index.html.

It is important to know the version of your browser to know which webdriver to install. To find out, use chrome://version/ in the Chrome browser. Once you have downloaded it, it is important to move the file to the Scripts folder in your Python directory.

Access a website

The first logical step is to be able to access a website. Three lines of code are all we need for this.

from selenium import webdriver

driver=webdriver.Chrome()
driver.get(“https://www.seleniumeasy.com/test/jquery-download-progress-bar-demo.html")

In the first line we import the library. In the second line we initialise the webdriver and in the last line we make a request to the url.

As a result, the browser will open at the requested url. The url used is one prepared for testing with Selenium. If you use another website you may be blocked if you run your code too many times in a row.

Accessing the url elements

We can now access any element of the website. To do this we need to right click on the element we want to use and see its html code. By right-clicking on the html element again we can copy its xpath.

We will use the find_element_by_xpath function to access the elements by code. When we access an element, we can perform functions on it. I show you an example of how I would click on an element with 2 lines:

my_element=driver.find_element_by_xpath(“//*[@id=’block-block-28']/div/div[2]/ul/li[1]/a”)
my_element.click()

We will also be able to send information to these elements. Here is another example where I write a text in a search engine:

searcher=driver.find_element_by_xpath(“//*[@id=’edit-search-block-form — 2']”)
searcher.send_keys(“codeless”)

Code to play

With the basic concepts I have explained, you can already do a lot of things with Selenium. Here below I leave a little code for you to play with it.

from selenium import webdriver

driver=webdriver.Chrome()
driver.get(“https://www.seleniumeasy.com/test/jquery-download-progress-bar-demo.html")

my_element=driver.find_element_by_xpath(“//*[@id=’block-block-28']/div/div[2]/ul/li[1]/a”)
my_element.click()

searcher=driver.find_element_by_xpath(“//*[@id=’edit-search-block-form — 2']”)
searcher.send_keys(“codeless”)

button_searcher=driver.find_element_by_xpath(“//*[@id=’search-block-form’]/div/div/div[1]/span/button”)
button_searcher.click()

1
$ 0.00
Avatar for jesgorbal
2 years ago

Comments