如何选择selenium web驱动程序中的下拉框

时间:2016-09-20 04:49:38

标签: java selenium webdriver

Select se = new Select(driver.findElement(By.xpath(".//*[@id='33629']/div/div[1]/div[2]/div[1]/select")));
se.selectByIndex(7);
driver.findElement(By.xpath(".//*[@id='33629']/div/div[1]/div[2]/div[1]/select/option[8]")).click();

以上代码不起作用,请帮助

返回错误:

Exception in thread "main" org.openqa.selenium.NoSuchWindowException: no such window: target window already closed from unknown error: web view not found

3 个答案:

答案 0 :(得分:1)

  

org.openqa.selenium.NoSuchWindowException:没有这样的窗口

表示当您尝试与其进行交互时,浏览器已关闭。从代码中删除driver.close(),只有在完成与浏览器的所有互动后才能将其放入。

  

修改

如果您需要在关闭子窗口后再次使用driver.switchTo()

返回父窗口
// get parent window ID
String parentHandle = driver.getWindowHandle();

// switch to the new window
for (String handle : driver.getWindowHandles()) {
    if (!handle.equals(parentHandle))
    {
        driver.switchTo().window(handle);
    }
}

//do something with the new window

// switch back to the old window
driver.close();
driver.switchTo().window(parentHandle);

答案 1 :(得分:-1)

windowIdbefore = driver.getWindowHandle();
System.out.println(windowIdbefore);
Set<String> windowid = driver.getWindowHandles();
for (String string : windowid) {
    System.out.println(string);
    driver.switchTo().window(string);
    // enter code here
}
WebDriver driver=new FirefoxDriver();
Select s=new Select(driver.findElement(By.xpath("xpathExpression")));
s.selectByVisibleText("text");
s.selectByValue("value");
s.selectByIndex(1);

答案 2 :(得分:-2)

正如我在这里看到的那样,下拉框出现在div标签中。我认为您的代码下拉列表已找到,但您无法选择下拉列表中的值。然后按照以下代码

WebDriverWait wait = new WebDriverWait(d, 10);
Actions builder = new Actions(d);

WebElement selectvalue = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("your drop down xpath value")));
builder.mouse.mouseMove(((Locatable)selectvalue).coordinates);      
selectvalue.click();

WebElement option = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("locator value of dropdown value(your dropdown value)")));
builder.mouse.mouseMove(((Locatable)option).coordinates);   
option.click();
System.out.println("dropdown value slected...");