简体   繁体   English

如何使用 Java 在 Selenium WebDriver 中选择和获取下拉值

[英]How to select and get dropdown value in Selenium WebDriver using Java

HTML Code is HTML 代码是

<select class="form_input_select bx-def-font" name="Sex[0]">
     <option value="Male">Man</option>                 
  <option value="Female">Woman</option>                   
<option value="Other" selected="selected">_Other</option>
    </select>

I am using below Selenium Java code:我在下面使用 Selenium Java 代码:

Select select = new Select(driver.findElement(By.name("Sex[0]")));
select.selectByIndex(0);
Thread.sleep(2000);

Cursor move on Man , But Man is not Show , Show only _Others光标在 Man 上移动,但 Man 不显示,只显示 _Others

Please help me for solve my issues I have applied more and more syntax but but I am not success to show Man...请帮我解决我的问题我已经应用了越来越多的语法,但我没有成功向 Man 展示...

you can use getText() to get selected text.您可以使用getText()获取选定的文本。

Select se=new Select(driver.findElement(By.name("Sex[0]")));
WebElement option = se.getFirstSelectedOption();
String gender=option.getText;

or use one of following options或使用以下选项之一

se.selectByVisibleText("Man");
se.selectByIndex(0);
se.selectByValue("Male");

Try to use :-尝试使用:-

se.selectByValue("Male");

OR

se.selectByVisibleText("Man");

OR

Use javascriptexecutor使用 javascriptexecutor

Hi think before selecting any option from the drop down please make all the options visible in the DOM so do it like below.嗨,在从下拉列表中选择任何选项之前,请让所有选项在 DOM 中都可见,所以请按照下面的方式进行操作。

driver.findElement(By.xpath("path to drop down upon click it will show 
the dd with values")).click();

Now once the options are visible on the page use the way you select an option form the DD现在,一旦选项在页面上可见,请使用您从 DD 中选择选项的方式

Select se=new Select(driver.findElement(By.name("Sex[0]")));
se.selectByIndex(0);
Thread.sleep(2000);

driver.findElement(By.name("Sex[0]")).sendKeys("Man"); driver.findElement(By.name("Sex[0]")).sendKeys("Man");

Finally i have found Solution Thanks everyone...最后我找到了解决方案谢谢大家...

To select any option from dropdown we have to click the dropdown element and select the desired option.要从下拉列表中选择任何选项,我们必须单击下拉元素并选择所需的选项。 Please find the below sample code:请找到以下示例代码:

WebElement gender = driver.findElement(By.name("Sex[0]"));
gender.click();
Select selectGender = new Select(gender);
selectGender.selectByValue("Male");
// or
// you can use any of below functions of Select class
selectGender.selectByIndex(0);
// or
selectGender.selectByVisibleText("Male");

Hope this helps希望这有帮助

You can use the below Code for Selecting the Values from Dropdown.您可以使用以下代码从下拉列表中选择值。

We have created the anonymous abject for Select .我们已经为Select创建了匿名对象。

new Select(driver.findElement(By.id("mainOrderForm:orderType"))).selectByVisibleText("Factory Order");

                        OR

new Select(driver.findElement(By.id("mainOrderForm:orderType"))).selectByIndex(Index_No.);

                        OR

new Select(driver.findElement(By.id("mainOrderForm:orderType"))).selectByValue("Value");

You can use the below code.您可以使用以下代码。 Just try atleast whether the selected option is getting printed on the console至少尝试是否在控制台上打印所选选项

    Select select = new Select(driver.findElement(By.name("Sex[0]")));
    select.selectByIndex(0);
    
    WebElement element = select.getFirstSelectedOption();
    System.out.println(element.getText());
                  or
    select.selectByVisibleText("Man");

暂无
暂无

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

相关问题 如何使用 Java 在 Selenium WebDriver 中选择隐藏的下拉值 - How to select a hidden dropdown value in Selenium WebDriver using Java 如何使用 Java 在 Selenium WebDriver 中选择下拉值 - How to select a dropdown value in Selenium WebDriver using Java 如何在 select 下拉值 Selenium WebDriver Java - How to select a dropdown value in Selenium WebDriver Java 如何使用Selenium WebDriver和java从下拉列表中选择项目? - How to select an item from a dropdown list using Selenium WebDriver with java? 如何使用 selenium webdriver 和 Java 从剑道下拉菜单中选择一个选项 - How to select an option from the kendo dropdown using selenium webdriver and Java 如何使用 selenium webdriver 从输入类型下拉列表中选择一个值? - How to select a value from input type dropdown using selenium webdriver? 当元素不是选择类型输入时,如何使用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:如何在范围下拉列表中选择一个值? - Selenium WebDriver: How to select a value in a span dropdown? 如何通过使用Java从Selenium Webdriver中的List中获取下拉列表值 - How to fetch dropdown value from List in selenium Webdriver by using java 如何使用Selenium WebDriver选择DropKick下拉菜单 - How to select dropkick dropdown using selenium webdriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM