Selenium Tutorials

Get Attribute values using Webdriver

There are cases where you want to get the attributes values and then perform any action.

In the below, if you see, button tag which has multiple attributes 'name', 'id', 'class' and 'aria-label' and has values for each attribute. To get the attribute value using selenium webdriver, we can use 'element.getAttribute(attributeName)'.

If we try to get the attribute value that doesn't exists for the tag, it will return null value.

Working with AutoComplete Text box

Now a days, in most of the applications, we can see a 'Auto Complete' textboxes which will help users to quickly find the option from a pre-populated list of values based on the text that is entered by the user. It mainly concentrates on providing suggestions to users while typing into the field.

Let us now see a basic example. When we enter any text, we can select the value from the pre-populated list by using 'String' or 'Index value'

auto complete textbox

Right Click Context Menu example

We will show how to work with context menu by taking a simple example. In the below example, we will first Right click on the element and the select the required option from the list of values. In the example we have also switched to an alert to verify if we have successfully clicked on the required link by using TestNG asserts

Simple Page Object Model example

In this example we will see a very simple Page Object Model example. To explain we have taken Google application and created BasePage, Sign In page and Create Account page.

To know what is page object model framework, Please refer Page Object Model Introduction
Steps to Create a Simple Page Object Model. The structure of the sample project should look like in the screen shot below

WebDriver Waits Examples

We can use WebDriverWait class in many different cases. When ever we need to perform any operation on element, we can use Webdriver wait to check if the element is Present or visible or enabled or disabled or Clickable etc.

NOTE - There are changes with Waits and Timeouts in Selenium 4, please check Updated Webdriver waits and timeouts.

We will look into different examples for all the above scenarios:

isElementPresent:

Drag and Drop using Webdriver Action Class

We have taken example program to perform drag and drop. In the below example, as the DragAndDrop divs are in a Frame, First we need to switch to the frame before performing drag and drop. And then we also need to check for the availability of SourceElement and DestinationElements.

Syntax for drag and drop
Actions action = new Actions(driver);
action.dragAndDrop(Sourcelocator, Destinationlocator).build().perform();

We can also make it as below:
(new Actions(driver)).dragAndDrop(element, target).perform();