Handle drop down and perform right click operation Selenium Web Driver

Handle drop down and perform right click operation Selenium Web Driver using java.

In this tutorial, we are going to focus on Drop down menu. How to Handle drop down and perform right click operation Selenium Web Driver using java.

Automate Drop down in selenium web driver with java.

To perform automation with drop down we need to know about select class in selenium web driver. you can see the HTML code in below image. The select element contains all the option element of drop down. So let’s understand the Select class in selenium.

Handle drop down with Selenium Web Driver

 

Select Class in Selenium web driver.

Select class or its methods is used to select or deselect options from a drop down. The select class takes an element of type WebElement as a constructor.

For Example:

// find all the elements which is avalible in Select tag
      WebElement elements = driver.findElement(By.id("select-demo"));
    
      //Create an object of Select class and pass the elements as parameter
    Select optionSelect = new Select(elements);

How to select option form drop down:

 selectByIndex – select by the index of option like 0,1,2 etc……….

//Select option by index ..... index start from 0.
    optionSelect.selectByIndex(0);

selectByValue – Select by the value of options.

//Select option by Value attribute.
    optionSelect.selectByValue("Monday");

selectByVisibleText – Select by GUI text of options.

//Select option by Text or GUI text.
    optionSelect.selectByVisibleText("Sunday");

Following are the useful methods of Select Class.

getOptions() – This method returns list of all the options(List<WebElement>).

getAllSelectedOptions() – This method will return list of all the selected options.

getFirstSelectedOption() – This method will return 1st selected option in case of multi-select.

isMultiple() – This method will return a true or false value (boolean value). It checks drop down is allow Multi select option or not.

deselectAll() – This method deselect all the selected options.

deselectByIndex(int index) – Deselect the option by its index.

deselectByValue(String valueAttribute) – Deselect the option its attribute value.

deselectByVisibleText(String text) – Deselect the option by GUI text or visible text.

Handle drop down with Selenium Web Driver

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class Main {
  public static void main(String[] args) throws InterruptedException {

    System.setProperty("webdriver.chrome.driver", "D:\\Automation\\selenium\\lib_files\\chrome\\chromedriver.exe");

    // Open Browser
    WebDriver driver = new ChromeDriver();

    // Open the URL in current window of the browser
    driver.get("http://www.seleniumeasy.com/test/basic-select-dropdown-demo.html");

    /************************ drop down ****************************************/
    // find all the elements which is avalible in Select tag
    WebElement elements = driver.findElement(By.id("select-demo"));

    // Create an object of Select class and pass the elements as parameter
    Select optionSelect = new Select(elements);

    // Select option by index ..... index start from 0.
    optionSelect.selectByIndex(0);

    // Select option by Text or GUI text.
    optionSelect.selectByVisibleText("Sunday");

    // Select option by Value attribute.
    optionSelect.selectByValue("Monday");

    // To get all the option from the drop down list.

    WebElement select_elements = driver.findElement(By.xpath("//*[@id=\"multi-select\"]"));
        
    // Pass element to select class
    Select select_option = new Select(select_elements);
    
    // Get the value of all the options and store in a list of type WebElement.
    List<WebElement> list = select_option.getOptions();

    // Retrieve the value from the list uisng loop
    for (WebElement l : list) {

      System.out.println(l.getText());
    }

  }
}

How to perform right click operation Selenium Web Driver

Sometimes we need to perform right click operation in selenium. To perform action form mouse and keyword we will use Action class in selenium. In this tutorial, we are going to learn How to perform right click operation in selenium web driver.

What is Action class in selenium please click on the below link and read about Action class => link.

Here is the simple example to perform right click operation is selenium web driver.

Example Scenario to be automated:

Open Browser

Launching the website URL to automate.

Find the element(Site Name Heading) by id or XPath it’s up to you.

Right click on the element.

Close browser.

perform right click operation in selenium web driver

Complete source code to perform right click operation using selenium web driver.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class Main {
  public static void main(String[] args) throws InterruptedException {

    System.setProperty("webdriver.chrome.driver", "D:\\Automation\\selenium\\lib_files\\chrome\\chromedriver.exe");

    // Open Browser

    WebDriver driver = new ChromeDriver();

    // Launching web site URL to automate.
    driver.get("http://www.seleniumeasy.com/test/");

    // Pass the driver object to the Action class.
    Actions action = new Actions(driver);

    // Find the element by id or xPath its up to you.
    WebElement siteLogo = driver.findElement(By.id("site-name"));

    // Pass the element to contextClick method and perform operation using object of
    // action class.
    action.contextClick(siteLogo).perform();

    // Close browser.
    // driver.close();
  }
}

 

Add a Comment

Your email address will not be published. Required fields are marked *