Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Java在SeleniumWebDriver中选择和获取下拉值_Java_Selenium_Selenium Webdriver - Fatal编程技术网

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

如何使用Java在SeleniumWebDriver中选择和获取下拉值,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,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="s

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>
光标移到人身上,但人不显示,只显示其他人

请帮助我解决我的问题,我已经应用了越来越多的语法,但我没有成功地向人展示…

您可以使用
getText()
获取所选文本

Select se=new Select(driver.findElement(By.name("Sex[0]")));
WebElement option = se.getFirstSelectedOption();
String gender=option.getText;
或者使用以下选项之一

se.selectByVisibleText("Man");
se.selectByIndex(0);
se.selectByValue("Male");
尝试使用:-

se.selectByValue("Male");


使用javascriptexecutor

在从下拉列表中选择任何选项之前,请先考虑一下,使所有选项在DOM中可见,这样做如下

driver.findElement(By.xpath("path to drop down upon click it will show 
the dd with values")).click();
现在,一旦选项在页面上可见,请使用从DD中选择选项的方式

Select se=new Select(driver.findElement(By.name("Sex[0]")));
se.selectByIndex(0);
Thread.sleep(2000);
driver.findElement(按名称(“性别[0]”)。sendKeys(“男子”)

我终于找到了解决办法
谢谢大家…

要从下拉列表中选择任何选项,我们必须单击下拉列表元素并选择所需的选项。请查找以下示例代码:

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");

希望这有帮助

您可以使用下面的代码从下拉列表中选择值

我们已经为
Select
创建了匿名abject

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");

您可以使用下面的代码。请至少尝试在控制台上打印所选选项

    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");
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");
    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");