Learning Selenium – Part 2

In previous tutorial, I just explained opening a website onto your browser with a script in python. To summarize, I explained what is selenium, motivation for its use, installation, and finally a simple code for poping up a browser loading a website.

Within this tutorial, I will explain general way to approach to write up a code to automate the browser and some example codes. Before going further, I would recommend to have a look over Automate the boring stuff website. It is cool blog that would discuss automating different computer modules using python.

Opening Your Browser’s Developer Tools

In addition to viewing a web page’s source, you can look through a page’s HTML using your browser’s developer tools. In Chrome and Internet Explorer for Windows, the developer tools are already installed, and you can press F12 to make them appear . In Chrome, you can also bring up the developer tools by selecting View▸Developer▸Developer Tools.

In Firefox, you can bring up the Web Developer Tools Inspector by pressing CTRLSHIFT-C on Windows and Linux . The layout is almost identical to Chrome’s developer tools.

After enabling or installing the developer tools in your browser, you can right-click any part of the web page and select Inspect Element from the context menu to bring up the HTML responsible for that part of the page. This will be helpful when you begin to parse HTML for your web scraping programs or finding element parameters that are commonly used in selenium.
That is whenever you have to fill out a form or simulate some click, the element corresponding to that tool must be intimated to selenium. Hence, above method helps to find elements using various categories.
WebDriver objects have quite a few methods for finding elements on a page. They are divided into the find_element_* and find_elements_* methods. The find_element_* methods return a single WebElement object, representing the first element on the page that matches your query. The find_elements_* methods return a list of WebElement_* objects for every matching element on the page.

Method name

WebElement object/list returned

Elements that use the CSS class name

Elements that match the CSS selector

Elements with a matching id attribute value

<a> elements that completely match the text provided

<a> elements that contain the text provided

Elements with a matching name attribute value

Elements with a matching tag name (case insensitive; an <a> element is matched by 'a' and 'A')

Once you have the WebElement object, you can find out more about it by reading the attributes or calling the methods below:

Attribute or method



The tag name, such as 'a' for an <a> element


The value for the element’s name attribute


The text within the element, such as 'hello' in <span>hello</span>


For text field or text area elements, clears the text typed into it


Returns True if the element is visible; otherwise returns False


For input elements, returns True if the element is enabled; otherwise returns False


For checkbox or radio button elements, returns True if the element is selected; otherwise returns False


A dictionary with keys 'x' and 'y' for the position of the element in the page

Programming a clicking of Page

WebElement objects returned from the find_element_* and find_elements_* methods have a click() method that simulates a mouse click on that element. This method can be used to follow a link, make a selection on a radio button, click a Submit button, or trigger whatever else might happen when the element is clicked by the mouse. For example, enter the following into the interactive shell:

The code is self explanatory, you just have to find web element corresponding to “official website”. So how did I find out? Just Inspect element over the interested web element and you will see a developer module opening in augmentation with your browser. Here, using function *find_element_by_link_text()* helps to select the one corresponding to ‘official website’ and with *click()* ,selenium simulates clicking of web page as a user. As simple as that, isn’t it?

Filling Out and Submitting Forms

Now with more on opening developer tools to find web elements, we can do much more advanced automation like login into gmail, facebook, yahoo, way2sms, etc.


# Find out the element corresponding to text field wherein string has to be passed using by id, text_link, name, or whatever is easy to catch.
# Using *send_keys(<string>)* pass string to corresponding text field one by one
# Finally to submit form, just use *submit()* function in conjunction with any element of text field you previously pointed out to submit the form. Calling the *submit()* method on any element will have the same result as clicking the Submit button for the form that element is in.



Example for logging into facebook:

Remember, to set the path to chromedriver executable file. Run the code and you must see chrome poping up. Then, it must load facebook and automatically input username and password and end up with clicking on submit button.

With next tutorial, I will get you more advances use of selenium. Until then keep calm and code for robotics ! But wait, robotics ? Ahh ? Yeah, you heard it right, this set of tutorials are going to help you with Home automation on raspberry pi. Such small details and python modules add up to give a fantastic home automation experience. Currently, I am using such modules to build up my own script of home automation over raspberry pi. I will soon post tutorial for my project here.


Leave a Reply, I generally respond quickly

%d bloggers like this: