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

browser.find_element_by_class_name(name)
browser.find_elements_by_class_name(name)

Elements that use the CSS class name

browser.find_element_by_css_selector(selector)
browser.find_elements_by_css_selector(selector)

Elements that match the CSS selector

browser.find_element_by_id(id)
browser.find_elements_by_id(id)

Elements with a matching id attribute value

browser.find_element_by_link_text(text)
browser.find_elements_by_link_text(text)

<a> elements that completely match the text provided

browser.find_element_by_partial_link_text(text)
browser.find_elements_by_partial_link_text(text)

<a> elements that contain the text provided

browser.find_element_by_name(name)
browser.find_elements_by_name(name)

Elements with a matching name attribute value

browser.find_element_by_tag_name(name)
browser.find_elements_by_tag_name(name)

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

Description

tag_name

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

get_attribute(name)

The value for the element’s name attribute

text

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

clear()

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

is_displayed()

Returns True if the element is visible; otherwise returns False

is_enabled()

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

is_selected()

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

location

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:

from selenium import webdriver
import os

chromedriver="/home/shubh/python/chromedriver"
os.environ["webdriver.chrome.driver"]=chromedriver
browser=webdriver.Chrome(chromedriver)

browser.get("http://shubhagrawal.in/2016/06/28/regular-expression-for-string-parsing/")
linkElem = browser.find_element_by_link_text('official website')
linkElem.click()

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.

Procedure:

# 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.

selenium_fb1

fb2

Example for logging into facebook:

from selenium import webdriver
import os

chromedriver="/path/to/chromedriver"
os.environ["webdriver.chrome.driver"]=chromedriver
browser=webdriver.Chrome(chromedriver)

browser.get("http://www.facebook.com")
emailEle=browser.find_element_by_id('email')
emailEle.send_keys('***<you email or phone no.*****')
passEle=browser.find_element_by_id('pass')
passEle.send_keys('****<your password>***')
passEle.submit()

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.

 

Comments are closed.

%d bloggers like this: