目前,我有这段代码。
Select dropdown = new Select(driver.findElement(By.id("pgList")));
dropdown.selectByVisibleText("TestCustomerGroup070615");
driver.findElement(By.id("select1")).click();
我还导入了导入org.openqa.selenium.support.ui.Select; ,但它似乎没有做它应该说的。
另外我认为应该包含我在打开新窗口时调用它:
String winHandleBefore = driver.getWindowHandle();
driver.findElement(By.id("addGlobal")).click();
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
Select dropdown = new Select(driver.findElement(By.id("pgList")));
dropdown.selectByVisibleText("TestCustomerGroup070615");
driver.findElement(By.id("select1")).click();
driver.close();
driver.switchTo().window(winHandleBefore);
什么似乎是问题?
弹出此错误:
Exception in thread "main" org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "div"
答案 0 :(得分:0)
尝试使用XPath定义WebElement并单击它:
By option = By.xpath(//select[@class = 'expression']/option[@value = 'value']);
driver.findElement(option).click();
答案 1 :(得分:0)
Select dropDown = new Select(driver.findElement(By.id("pgList")));
并在其中一种方法之后选择一些需要的选项:
dropDown.SelectByText("some item text");
dropDown.SelectByIndex(2);
dropDown.SelectByValue("SomeValue");