[英]How to extract the text of the firstselectedoption from a dropdown using Selenium Webdriver through Java
After selecting an option from drop-down.从下拉列表中选择一个选项后。 I am trying to get that option displayed in the console.
我试图让该选项显示在控制台中。 below is my code.
下面是我的代码。 But I get
但我得到
"//[[[[ChromeDriver: chrome on WINDOWS (d5a01776981da5dacfeb89dbbc2e6b52)] -> xpath: //*[@name='airline']]].// -> tag name: option]"
The tag name is option for dropdown options.标签名称是下拉选项的选项。 I have tried all solutions of selectByXXXX.
我已经尝试了 selectByXXXX 的所有解决方案。 but nothing seems to work.
但似乎没有任何效果。 what would be the right code ?
什么是正确的代码?
//airline preference
{
Select airline = new Select (driver.find Element(By.name("airline"))); //selecting tag
Thread.sleep(2000); //sleeptime`
airline.selectByVisibleText("Pangea Air"); //selecting option
Thread.sleep(2000); //sleep time
Select airlin = new Select (driver.findElement(By.xpath("//*[@name='airline']"))); //omg
WebElement s = airlin.getFirstSelectedOption();
Thread.sleep(2000);
System.out.println(s);
}
getFirstSelectedOption()
returns the first selected option in this select tag (or the currently selected option in a normal select). getFirstSelectedOption()
返回此选择标记中的第一个选定选项(或普通选择中当前选定的选项)。 NoSuchElementException
is thrown if no option is selected.如果没有选择任何选项,则抛出
NoSuchElementException
。
Seems you were pretty close.看来你已经很接近了。 Once you select an option through
selectByVisibleText()
next you can invoke getFirstSelectedOption()
to choose the option element selected and finally using getText()
you can extract the option text as per the solution below:一旦您通过
selectByVisibleText()
选择了一个选项,接下来您可以调用getFirstSelectedOption()
来选择所选的选项元素,最后使用getText()
您可以按照以下解决方案提取选项文本:
Code Block:代码块:
Select airline = new Select (driver.find Element(By.name("airline"))); //selecting tag airline.selectByVisibleText("Pangea Air"); //selecting option WebElement s = airline.getFirstSelectedOption(); System.out.println(s.getText());
Console Output:控制台输出:
Pangea Air
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.