Select is a class provided by Selenium that is used to work with a dropdown element.
The select class allows us to select an element from the drop-down and lists that are created with the HTML select element.
The select class is present in org.openqa.selenium.support.ui package. It extends Object class and implements the ISelect interface.
The general syntax to declare Select class in Selenium is as follows:
public class Select implements ISelect, org.openqa.selenium.WrapsElement
How to create Object of Select Class in Selenium?
===========================================
|-Select is an ordinary class, its object is created by using the new keyword. |-|-After creating object, we will have to pass the dropdown WebElement as parameter to its constructor.
|-The syntax to create an object of select class is as follows:
Select select=new Select(WebElement webelement)
Example:-
package base;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import com.fasterxml.jackson.databind.annotation.JsonAppend.Prop;
import io.github.bonigarcia.wdm.WebDriverManager;
public class BaseTest {
public WebDriver webDriver;
public FileReader fileReader;
public Properties properties=new Properties();
@BeforeTest
public void setUp() throws IOException {
if(webDriver==null) {
String path= System.getProperty("user.dir")+"\\src\\test\\resources\\config\\configuration.properties";
fileReader=new FileReader(path);
properties.load(fileReader);
}
if(properties.getProperty("browser").equals("chrome")) {
//set the ChromeDriver
WebDriverManager.chromedriver().setup();
//open the Browser
webDriver=new ChromeDriver();
}
else if(properties.getProperty("browser").equals("edge")) {
//set the EdgeDriver
WebDriverManager.edgedriver().setup();
//open the browser
webDriver=new EdgeDriver();
}
else if(properties.getProperty("browser").equals("Firefox")) {
//set up the FireFox Driver
WebDriverManager.firefoxdriver().setup();
//open the browser
webDriver=new FirefoxDriver();;
}
//Load a new web page in the current browser window
webDriver.get(properties.getProperty("testUrl"));
}
@AfterTest
public void tearDown() {
//close the browser
webDriver.close();
}
}
package testing;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;
import base.BaseTest;
public class Select_DropDownListTester extends BaseTest{
@Test
public void Select_DropDownListTest() {
//Locate dropdown element on web page By.xpath
WebElement dropdown= webDriver.findElement(By.xpath("//select[@name='country']"));
Select select=null;
//Verify the dropdown is enabled and visible
if(dropdown.isEnabled() && dropdown.isDisplayed()) {
System.out.println("Drop Down is enabled and visible");
//create an object of Select class and pass the dropdown of type WebElement as an argument
select=new Select(dropdown);
//verify that dropdown does not allow the multiple selections
if(select.isMultiple()) {
System.out.println("Drop down list accepts mulitple choices");
}
else {
System.out.println("Drop down list does not accept multiple choices");
}
int listsize= select.getOptions().size();
System.out.println("listsize: "+listsize);
//select the option "india" by sending visible text
//select.selectByVisibleText("India");
//select the option "USA" by sending its value attribute
//select.selectByValue("US");
select.selectByIndex(1);
//check that "india" is selected as an option or not
String text= select.getFirstSelectedOption().getText();
System.out.println("Option chosen: "+text);
}else {
System.out.println("Drop Down is not enabled and visible");
}
}
}
En esta página del sitio puede ver el video en línea Select Class in Selenium | Select Class Methods de Duración hora minuto segunda en buena calidad , que subió el usuario Generative Learning 19 diciembre 2023, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 30 veces y le gustó 0 a los espectadores. Disfruta viendo!