In this tutorial, we’ll teach you about how to perform Navigation using Selenium Python. More importantly, you’ll get to learn how to interact with a page and its elements such as input text, buttons, and drop-downs.
After successfully opening a website, what do we wish to do next? We usually navigate to other pages. Mostly, the home page contains navigation links which we click and move to another web page. Alternatively, we can use the site search and find the pages of our interest.
Let’s now see what different navigation methods Selenium Python provides and how to use them.
Navigation: Crawl Web Pages using Selenium Python
The WebDriver provides “get()” method to open a web page.
WebDriver returns the control to the test script after the page gets fully loaded. However, if the web page uses a lot of AJAX, the WebDriver may not be able to determine, when it has loaded completely. For such pages, we can use WebDriver <waits> to ensure that the web page gets loaded completely.
While filling a web form, we may have to work with the following HTML elements.
a) Textarea and text field
While navigating a page, we may also be interested in performing different actions on it. There are various HTML elements present on a web page. We can do different operations on them.
WebDriver requires finding that element first before it can perform any action on it.
For example, suppose the first element to search is a text field.
<input type="text" name="user" id="user_name" />
We can find it using either of the following methods:
element = driver.find_element_by_id("user_name") element = driver.find_element_by_name("user") element = driver.find_element_by_xpath("//input[@id='user_name']")
If the given XPath leads to a list of item, then the above command will return the first match.
While, if the element is a link, we can search it using the link text. We have to ensure that the search string is exactly same as the link text. However, if the search fails, then Webdriver will throw NoSuchElementException.
After finding the element, we will enter some text in the text field as:
Moving up an down using arrow keys in a multi-line text box can be achieved through “Keys” class as:
element.send_keys(" and some", Keys.ARROW_DOWN)
We can use send_keys on any element in a similar manner. Consider the following facts about this method:
It simulates keystrokes on the elements test keyboard shortcuts such as those present in Gmail.
Whatever message we send via ‘send_keys’ gets appended to what’s already there. It does not automatically clear the text that is already present there. WebDriver provides a <clear> method that helps to clear the contents of a text field or textarea as:
So far, we’ve seen how to enter text into a textarea or text field, but there are other elements also such as a drop-down.
The <select> tag is an HTML element which creates a drop-down list on the web page. This tag also encapsulates an <option> tag which defines the items in the list.
Refer the following sample HTML code for drop-down.
<form action="/submit.php"> <select name="Python numbers"> <option value="int">Integer</option> <option value="float">Float</option> <option value="long">Long</option> <option value="complex">Complex</option> </select> <br><br> <input type="submit"> </form>
I) Selecting Options From Dropdown
With Selenium Python, there come two ways to interact with the drop-down list.
- In the first case we locate the drop-down element by using either of “find_element_by_xpath”, or “find_element_by_id” functions. Now we can retrieve the list of options in the drop-down using “find_elements_by_tag_name.” Now we can iterate through the options present in the list.
Let’s take a look at the code snippet for the same.
element = driver.find_element_by_xpath("//select[@name='Python numbers']") all_options = element.find_elements_by_tag_name("option") for option in all_options: print("Value is: %s" % option.get_attribute("value")) option.click()
The above code will print the values under each option. <option.click()> method allows selecting a particular option.
- The other way is the recommended one. Selenium Python API provides the Select class, which contains useful methods for handling these <select> web elements. We’ll discuss each one by one.
i) Using Index of Drop-down
If the drop-down has an “index” attribute, then we can use that index to select a particular option. Selenium Webdriver provides the “select_by_index( index )” method to select an option using the index attribute.
It throws “NoSuchElementException,” If there is no option with the specified index. For example,
element = Select(driver.find_element_by_id('id_of_element')) element.select_by_index(2)
ii) Using the Value of Drop-down
Usually, the option tag in HTML comes with the value attribute. In that case, we use “select_by_value(value )” method to select an item from the drop-down matching to the value.
Suppose the HTML for the drop-down is like this
then the following code helps to select by value
element = Select(driver.find_element_by_id('id_of_element')) element.select_by_value('int')
iii) Using Text of Dropdown
It can be done by using “select_by_visible_text(text)” method. It will select the item from the drop-down, that matches with the text given in the method.
element = Select(driver.find_element_by_id('id_of_element')) element.select_by_visible_text('element_text')
So far we have discussed various methods to select an item from the drop-down. There may be situations when we have to remove the selections. In the coming section, we will cover commands for the same.
II) De-Selecting Options From Dropdown
This method enables us to clear all the selected options. It is useful when we have selected multiple items from the drop-down.
If we use this method in case of single selection, it will throw a NotImplementedError exception.
This API clears the selected option using the “index” attribute. It is the inverse of the select_by_index( ) method.
This API clears the selected option using the value of the option. It is the inverse of the select_by_value( ) method.
This API clears the selected option using the text of the option. It is the inverse of the select_by_visible_text( ) method.
The Select class returns a reference to the drop-down element which contains a list of items. To traverse through all the objects in the list, Select provides “options” property.
Let’s now see a sample code that will show, how all this works in reality.
from selenium import webdriver from selenium.webdriver.support.select import Select import time driver = webdriver.Firefox() driver.maximize_window() driver.get('http://www.toolsqa.com/automation-practice-form/') s1 = Select(driver.find_element_by_id('continents')) s1.select_by_visible_text('Europe') for opt in s1.options: print(opt.text) s1.select_by_visible_text(opt.text) time.sleep(10)
The above snippet moves the control to the “Continents” drop-down in the web page. After that, it selects all the items present in the drop-down, one by one.
Back to – Selenium Python Tutorial
Quick wrap up – Navigation using Selenium Python
Navigation is an important concept as it lets you flow through the different sections of a website. You’ve now moved up another step in using the Selenium with Python.