Handle dropdowns in Selenide

Tutorial 14​​ –​​ Handle dropdowns​​ in Selenide

What you will Learn in this blog:

  • selectOption() by integer

  • selectOption() by value

  • selectOptionByValue() by attribute value

  • Code snippet

selectOption() by integer

Go to​​ https://www.way2automation.com/way2auto_jquery/dropdown.php#load_box​​ 

There is a ‘Select Country’ dropdown

​​ 

Let​​ us see how to select a country from dropdown. The dropdown is embedded inside a frame

Inspect the dropdown field

There​​ is a ‘selectOption()’​​ method that can be used to select a value from dropdown

Execute, notice below that the 5th​​ option gets selected from dropdown

selectOption() by value

We can also select a specific value

So we can write

Execute

selectOptionByValue() by attribute value

We can also select a specific ‘attribute’ value

So we can write

Execute

Code snippet

package​​ w2a.selenide;

 

import​​ static​​ com.codeborne.selenide.Selenide.*;

import​​ static​​ com.codeborne.selenide.Selenide.open;

 

import​​ java.net.MalformedURLException;

import​​ java.util.List;

 

import​​ org.openqa.selenium.By;

import​​ org.testng.annotations.Test;

 

import​​ com.codeborne.selenide.Configuration;

import​​ com.codeborne.selenide.ElementsCollection;

 

public​​ class​​ ElementsTest {

@Test

public​​ void​​ searchTest()​​ throws​​ InterruptedException, MalformedURLException {

 Configuration.browser​​ =​​ "firefox";

open("https://www.way2automation.com/way2auto_jquery/dropdown.php#load_box");

switchTo().frame($(By.cssSelector(".demo-frame")));

$(By.cssSelector("body:nth-child(2) > select:nth-child(1)")).selectOption(5);

$(By.cssSelector("body:nth-child(2) > select:nth-child(1)")).selectOption("Bulgaria");

$(By.cssSelector("body:nth-child(2) > select:nth-child(1)")).selectOptionByValue("Cyprus");

 Thread.sleep(6000);

 }

}

 

Thank you for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

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

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 10th Nov 2024