How to handle dropdowns in selenium?

0 votes
I am new to selenium and trying to learn ways of handling elements. I tried working on facebooks signup page. The date of birth section find by .sendKey("Value') but that is not the correct way. Can someone help me with the appropriate way of handling the dropdown? Thank you!
Jan 21 in Selenium by Rishab
• 1,460 points
44 views
can you please share the code as what are you trying to do?

2 answers to this question.

0 votes

There are three ways of handling the drop down:-

Select countriesDropDown = new Select(driver.findElement(By.id("day")));
dropdown.selectByVisibleText("6");

dropdown.selectByIndex(5);//generally index starts with 0

dropdown.selectByValue("6");
answered Jan 21 by trisha
0 votes

Hey @Rishab, you can take a look at the following code.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class AmazonShoping {

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

        //You mention the path to your chromedriver or geckodriver
        System.setProperty("webdriver.chrome.driver","C:\\Users\\priyj_kumar\\Downloads\\chromedriver.exe");

        //Create an instance of your chromedriver named driver
        WebDriver driver = new ChromeDriver();

        //Open facebook
        driver.get("https://www.facebook.com");

        //Create an instance of select and specify the webelement
        org.openqa.selenium.support.ui.Select dropdown = new org.openqa.selenium.support.ui.Select(driver.findElement(By.id("day")));

        // 1. You can select by value
        dropdown.selectByValue("6");

        // 2. You can select by index, you need to verify the index as it starts with 0 or 1
        dropdown.selectByIndex(6);

        // 3. You can select by Visible Text
        dropdown.selectByVisibleText("6");

    }

}

Hope this helps you. 

answered Jan 23 by Priyaj
• 56,520 points

Related Questions In Selenium

0 votes
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,090 points
1,974 views
0 votes
1 answer

How to handle Pop-up in Selenium WebDriver using Java

Actually, its pretty simple. Use this code ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,090 points
4,278 views
0 votes
1 answer

How to handle notifications in Python with Selenium (Chrome WebDriver)

Below will help you: You can disable the ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
2,274 views
0 votes
1 answer

How to handle chrome notification in selenium?

The pop-ups can be irritating but it ...READ MORE

answered Jan 2 in Selenium by Nabarupa
742 views
0 votes
1 answer

How to handle IE protected mode zone and zoom level setting in selenium C#

For changing the zoom level you can ...READ MORE

answered Feb 13 in Selenium by Priyaj
• 56,520 points
717 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,090 points
568 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
895 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
559 views
0 votes
1 answer

HTML unit webdriver in Selenium

You simply need to create instance for ...READ MORE

answered Jan 21 in Selenium by abhinav
52 views