Java 如何使用SeleniumWebDriver从下拉列表中的选定选项将文本转换为字符串变量?

Java 如何使用SeleniumWebDriver从下拉列表中的选定选项将文本转换为字符串变量?,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,这就是我正在使用的。但它不起作用。请帮助我。如果要从下拉列表中获取当前选定的文本,可以使用“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

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

如果要从下拉列表中获取当前选定的文本,可以使用“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.selectByValue(cityName);

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

太好了。不客气。如果我的回答符合您的期望,请您接受我的回答并将其标记为有用:-)。
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();