如何使用selenium webdriver从下拉列表中的选定选项中获取文本到字符串变量?

时间:2016-12-05 23:28:53

标签: java selenium selenium-webdriver

String city;
Select dropdown = new Select(driver.findElement(By.id("city"))); //Selects the dropdown menu 
dropdown.selectByValue(cityName); //selects Ajax as the city from the Dropdown menu
city = dropdown.selectByValue(cityName);

这就是我正在使用的。但它不起作用。请帮帮我。

1 个答案:

答案 0 :(得分:0)

如果您想从下拉列表中获取当前选定的文字,可以使用' getFirstSelectedOption'方法,如下图所示:

String city;
Select dropdown = new Select(driver.findElement(By.id("city")));     //Selects the dropdown menu 
dropdown.selectByValue(cityName); //selects Ajax as the city from the Dropdown menu
city = dropdown.getFirstSelectedOption();

如果我误解了你的问题,请告诉我。