2015-07-06 92 views
-1

目前,我有这个代码。选择硒中的下拉项目

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

什么似乎是问题? – FazoM

+0

它打开新窗口,但根本没有做任何事情。 –

+0

您是否在新窗口或主窗口上执行操作。先关闭主窗口,然后切换到新窗口。然后继续在新窗口中进行操作。 –

回答

0

尝试使用XPath来定义WebElement并单击它:

By option = By.xpath(//select[@class = 'expression']/option[@value = 'value']); 
driver.findElement(option).click(); 
0
Select dropDown = new Select(driver.findElement(By.id("pgList"))); 

以及这些方法来选择需要一些备选方案一后:

dropDown.SelectByText("some item text"); 

dropDown.SelectByIndex(2); 

dropDown.SelectByValue("SomeValue");