简体   繁体   中英

how to select the drop down box in selenium web driver

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

Above code doesn't work,please help

Error returned:

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

org.openqa.selenium.NoSuchWindowException: no such window

Means the browser is close when you are trying to interact with it. Remove driver.close() from your code and put it only after you have finished all you interactions with the browser.

Edit

If you need to return to parent window after closing child window use driver.switchTo() again

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

as i see here the dropdown box is present in div tag. i think with your code dropdown has been located but you are not able to select the value present in dropdown. Then follow below code

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM