How to select item in dropdown?
We know there are two type of select in HTML. Single select and multiple select.We use Select class in java code to get Select object and use 3 possible methods to select items.
All 3 possible methods are given below.
1.select.selectByIndex(3)
2.selectByValue("valuexyz")
3.selectByVisibleText("xyz")
Example in java:-
" WebElement element2 =driver.findElement(By.id(""fruits""));Select fruits =new Select(element2);
fruits.selectByIndex(6);
fruits.selectByValue(""mango"");
fruits.selectByVisibleText(""Kivi"");
"
Complete Select dropdown example in java code:- Single and multiple select both
"package pkg1;
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 MyClass9_selenium_dropdown {
public static void main(String[] args) {
System.setProperty(""webdriver.chrome.driver"",""C:\\Users\\Desk-105\\Documents\\SELENIUM\\chromedriver.exe"");
WebDriver driver = new ChromeDriver();
driver.get("https://www.onlinetutorial.info/p/selenium-dropdown-select.html");
/**
* single select example
*/
WebElement element1 =driver.findElement(By.id(""gender""));
Select genders =new Select(element1);
genders.selectByVisibleText(""Other"");
/**
* multiple select example
*/
WebElement element2 =driver.findElement(By.id(""fruits""));
Select fruits =new Select(element2);
fruits.selectByIndex(6);
fruits.selectByValue(""mango"");
fruits.selectByVisibleText(""Kivi"");
}
}
"
No comments:
Post a Comment