我无法在上面提到的代码中选择下拉值
public class Dropdown {
public static void main(String[] args) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "I:/shopclues/demoqa/drivers/chromedriver.exe");
ChromeDriver driver= new ChromeDriver();
driver.get("http://demoqa.com/");
driver.manage().window().maximize();
driver.findElementByLinkText("Registration").click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement country = driver.findElementByXPath("//select[@id='dropdown_7']");
Select dropdown = new Select(country);
List<WebElement> options = dropdown.getOptions();
for (WebElement option1 : options)
{
System.out.println(option1.getText());
if(option1.getText().startsWith("india"))
{
option1.click();
break;
}
}
}
}
由于
答案 0 :(得分:1)
根据gihan's answer here,您可以通过以下方式进行选择:
dropdown .selectByVisibleText(“India”); dropdown.selectByIndex(0); dropdown.selectByValue( “印度”);
出于您的目的,第一个选项应该可以正常工作。
答案 1 :(得分:1)
使用以下方式选择下拉列表 -
WebElement country = driver.findElement(By.id("dropdown_7"));
Select dropdown = new Select(country);
dropdown.selectByVisibleText("india"); // pass the text which is visible on site
//or
dropdown.selectByIndex(1); // pass the index of dropdown value you want to select
//or
dropdown.selectByValue("your_value");
或更改您的代码
WebElement country = driver.findElementByXPath("//select[@id='dropdown_7']");
到
WebElement country = driver.findElement(By.id("dropdown_7"));