Lightning talk #13 – Using Selenium for web automation

Selenium is a web automation tool that allows control of the browser. Combined with python, we can do great stuff in only a few lines.

Let’s take some examples.

For example, this one will simply open firefox and go to facebook.com, and then close itself.

If we want to write some data into a webpage field, we just need to find the element we want to write into and pass it some keys.

Complete example

Let’s say we want to login on facebook – what do we usually do? We open a browser, go to facebook.com, write the credentials, and then press ok. Those are exactly the steps for doing that with selenium.

This will open firefox and login on facebook. For that, we will need geckodriver (to control firefox), or chromedriver (to control google chrome).

We can also further write a shell script to open that python script for us, with some set credentials.

find_element_by_

What else we can do with selenium? Well, the biggest feature is that we can find elements in the webpage. How we can do that?

find_element_by_id

find_element_by_name

find_element_by_link_text

find_element_by_tag_name

find_element_by_class_name

find_element_by_css_selector

Anything else?

What else you can do with selenium other than finding elements? Well, you can also wait until elements are visible before getting them (wait for page to load), you can manipulate text fields like any other human (write some text, write letter by letter, with random delay between characters, for example), you can press keys and key combinations, handle popups, close tabs, add new tabs, execute javascript, drag and drop events, and so much more. Prety much anything that you can do manually.

You may also like...

Leave a Reply