简体   繁体   English

如何使用 Selenium WebDriver 和 Java 获取选定的选项

[英]How to get selected option using Selenium WebDriver with Java

I want to get the selected label or value of a drop down using Selenium WebDriver and then print it on the console .我想使用 Selenium WebDriver获取选定的 label下拉列表的值,然后将其打印控制台上。

I am able to select any value from the drop down, but I am not able to retrieve the selected value and print it:我可以 select 下拉列表中的任何值,但我无法检索所选值并打印它:

Select select = new 
Select(driver.findElement(By.id("MyDropDown"))).selectByVisibleText(data[11].substring(1 , data[11].length()-1));
WebElement option = select.getFirstSelectedOption();

But all my efforts were in vain.但是我所有的努力都是徒劳的。 How do I get the selected option?我如何获得选择的选项?

You should be able to get the text using getText() (for the option element you got using getFirstSelectedOption() ):您应该能够使用getText()获取文本(对于您使用getFirstSelectedOption()获得的选项元素):

Select select = new Select(driver.findElement(By.xpath("//select")));
WebElement option = select.getFirstSelectedOption();
String defaultItem = option.getText();
System.out.println(defaultItem );

Completing the answer:完成答案:

String selectedOption = new Select(driver.findElement(By.xpath("Type the xpath of the drop-down element"))).getFirstSelectedOption().getText();

Assert.assertEquals("Please select any option...", selectedOption);

In Selenium Python it is:在 Selenium Python 中,它是:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select

def get_selected_value_from_drop_down(self):
    try:
        select = Select(WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.ID, 'data_configuration_edit_data_object_tab_details_lb_use_for_match'))))
        return select.first_selected_option.get_attribute("value")
    except NoSuchElementException, e:
        print "Element not found "
        print e

On the following option:在以下选项上:

WebElement option = select.getFirstSelectedOption();
option.getText();

If from the method getText() you get a blank, you can get the string from the value of the option using the method getAttribute :如果从方法getText()得到一个空白,则可以使用方法getAttribute从选项的值中获取字符串:

WebElement option = select.getFirstSelectedOption();
option.getAttribute("value");

short answer简答

Select select = new Select(driver.findElement(By.xpath("//select")));
System.out.println("selected items from the dropdown"+ select.getFirstSelectedOption().getText());
var option = driver.FindElement(By.Id("employmentType"));
        var selectElement = new SelectElement(option);
        Task.Delay(3000).Wait();
        selectElement.SelectByIndex(2);
        Console.Read();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用带有Java的Selenium WebDriver获取选择的选项 - Get selected option using Selenium WebDriver with Java 当元素不是选择类型输入时,如何使用Selenium Webdriver Java从下拉列表中获取所有选项值? - how to get all option values from dropdown using selenium webdriver java when the element is not a select type input? 如何使用 selenium webdriver 和 Java 从剑道下拉菜单中选择一个选项 - How to select an option from the kendo dropdown using selenium webdriver and Java 如何使用Selenium WebDriver(2)从dojo组合框中获取选定的值 - How to get the selected value from a dojo combobox using Selenium WebDriver(2) 如何使用 selenium java 从下拉列表中打印选定的选项? - How to print selected option from drowdown by using selenium java? 如何通过使用Selenium WebDriver Java来选择是否测试或验证复选框 - How to test or verify check box is selected or not selected by using selenium webdriver java 如何使用Java使用Selenium WebDriver获取所有跨度值? - How to get all the span value with Selenium WebDriver using Java? 如何使用Java使用Selenium Webdriver在此html中获取标记值? - How to get the <b> tag value in this html with Selenium Webdriver using Java? 如何使用Selenium WebDriver + Java获取浏览器控制台错误消息? - How to get browser console error messages using Selenium WebDriver + Java? 如何在Java中使用Selenium Webdriver获取更新的隐藏字段值 - how to get updated hidden field value using Selenium webdriver in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM