TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Web Page Navigation in Selenium Python
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Python SeleniumPython Tutorials

Web Page Navigation in Selenium Python

Last updated: Sep 30, 2023 7:19 pm
By Meenakshi Agarwal
Share
9 Min Read
Navigation Selenium Python Tutorial
Navigation Selenium Python Tutorial
SHARE

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.

Contents
Navigation MethodInteracting with Textarea and text fieldInteracting with Drop-downSelect Dropdown OptionsDe-Select Options From Dropdown

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 in Selenium Python

Navigation Method

The WebDriver provides a “get()” method to open a web page.

driver.get("http://www.google.com")

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 out a web form, we may have to work with the following HTML elements.

Interacting with 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 items, 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 the 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:

element.send_keys("message")

Moving up and down using arrow keys in a multi-line text box can be achieved through the “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 and tests 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:

element.clear()

Interacting with Drop-down

So far, we’ve seen how to enter text into a text area or text field, but there are other elements 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 a <option> tag which defines the items in the list.

Refer to 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>

Select Dropdown Options

With Selenium Python, there are two ways to interact with the drop-down list.

Using Find Element API

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.

Using Select API

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.

Using the 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)
Using the Value of Drop-down

Usually, the option tag in HTML comes with the value attribute. In that case, we use the “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

 <option value="int">Integer</option>

then the following code helps to select by value

element = Select(driver.find_element_by_id('id_of_element'))

element.select_by_value('int')
Using Text of Dropdown

It can be done by using the “select_by_visible_text(text)” method. It will select the item from the drop-down, that matches with the text given in the method.

For example,

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.

De-Select Options From Dropdown

deselect_all( )

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 a single selection, it will throw a NotImplementedError exception.

deselect_by_index( )

This API clears the selected option using the “index” attribute. It is the inverse of the select_by_index( ) method.

deselect_by_value( )

This API  clears the selected option using the value of the option. It is the inverse of the select_by_value( ) method.

deselect_by_visible_text( )

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 the “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 on the web page. After that, it selects all the items present in the drop-down, one by one.

Back to – Selenium Python Tutorial

Summary – 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 Selenium with Python.

For more updates on Selenium Python tutorials, do follow our social media accounts to get free access to all our future updates.

Best,

TechBeamers

You Might Also Like

How to Connect to PostgreSQL in Python

Generate Random IP Address (IPv4/IPv6) in Python

Python Remove Elements from a List

How to Use Extent Report in Python

10 Python Tricky Coding Exercises

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article 35 TestNG Interview Questions and Answers for Sure Success TestNG Interview Questions for Sure Success in 2024
Next Article Switch Between Windows Selenium Python How to Switch Between Windows Using Selenium Python

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x