If a Drop Down is made of Select tag then you can use Select class of Selenium.
Select select = new Select(WebElement);
select.selectByIndex(int index);
select.selectByValue(String value);
select.selectByVisibleText(String text);
If it is made of Divs and spans then you might wanna use this code :
List<WebElement> options = driver.findElements(by.xpath(" your locator"));
for(WebElement element : options){
if(element.getText().equals(" your value from drop down")){
element.click();
}
}
Update :
HTML File :
<html>
<head>
<title>StackOverFlow Problems </title>
</head>
<body>
<select id="form1:PartialSysAdminKey_adminContractIdField" name="form1:PartialSysAdminKey_adminContractIdField" class="selectOneMenu" size="1">
<option value=" "></option>
<option value="Lstitem1">List item1</option>
<option value="Lstitem2">List item2</option>
<option value="Lstitem3">List item3</option>
</select
</body>
</html>
Automation code using Java + Selenium :
public class Mike {
static WebDriver driver;
static WebDriverWait wait;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "D:\Automation\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
wait = new WebDriverWait(driver, 20);
driver.get("file:///C:/Users/HunteR/Desktop/Automation/abc.html");
Thread.sleep(3000);
Select select = new Select(driver.findElement(By.cssSelector("select[id*='adminContractIdField']")));
select.selectByValue("Lstitem3");
}
}
It is working mightily fine on my machine. Please let me know if you have any concerns related to this.
Note :
Thread.sleep(3000) was used in my code for visualization purpose.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…