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 22, 2019 in Selenium by Rishab
• 1,490 points
1,034 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 22, 2019 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, 2019 by Priyaj
• 58,020 points

Related Questions In Selenium

+1 vote
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,500 points
8,381 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 6, 2018 in Selenium by nsv999
• 5,500 points
10,582 views
+1 vote
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,910 points
14,458 views
0 votes
2 answers

How to handle chrome notification in selenium?

Map<String, Object> prefs = new HashMap<String, Object>(); prefs.put("profile.default_content_setting_values.notifications", ...READ MORE

answered May 4, 2020 in Selenium by Mukti
• 140 points
5,809 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, 2019 in Selenium by Priyaj
• 58,020 points
4,451 views
0 votes
2 answers

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,500 points
13,728 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,168 views
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,910 points
7,245 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,886 views
0 votes
1 answer

HTML unit webdriver in Selenium

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

answered Jan 22, 2019 in Selenium by abhinav
1,198 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP