简体   繁体   English

如何通过 Java 使用 Selenium Webdriver 从下拉列表中提取 firstselectedoption 的文本

[英]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 getFirstSelectedOption

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.

相关问题 如何通过Java使用Selenium从元素中提取文本 - How to extract the text from the element using Selenium through Java 如何使用Selenium WebDriver和Java通过匹配文本模式从下拉列表中选择选项 - How to select an option from dropdown list by matching text pattern using selenium webdriver and java 如何使用 selenium webdriver 和 Java 从剑道下拉菜单中选择一个选项 - How to select an option from the kendo dropdown using selenium webdriver and Java 如何使用Selenium WebDriver和java从下拉列表中选择项目? - How to select an item from a dropdown list using Selenium WebDriver with java? 如何通过使用Java从Selenium Webdriver中的List中获取下拉列表值 - How to fetch dropdown value from List in selenium Webdriver by using java 如果我通过Selenium Webdriver和Java知道相应的文本,如何提取DOM元素的ID属性 - How to extract the ID attribute of a DOM element, if i know corresponding text through Selenium Webdriver and Java 如何使用 Selenium WebDriver 和 Java 从元素中检索文本 - How to retrieve text from an element using Selenium WebDriver and Java 如何通过与Java一起使用Selenium WebDriver从此HTML代码中获取文本 - How to get Text from this HTML code By using Selenium WebDriver with Java 如何使用Selenium WebDriver和Java从图像(验证码)中读取文本 - How to read the text from image (captcha) by using Selenium WebDriver with Java 如何通过Selenium WebDriver从下拉菜单中选择一个选项 - How to select an option from a dropdown through Selenium WebDriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM