Element should have been "select" but was "li"

时间:2017-11-08 22:07:10

标签: java arrays selenium

I am trying to get selenium to go thought the drop down and verify every single option within the drop drop. I have tried using the Xpath, but still no luck. Also if there's a way to clean up my array without hard coding all the options that would be great. The following is my code:

    Googledriver.get("http://www.lolesports.com/en_US/"); 
    Thread.sleep(3000);

    String arr[]= {"NA LCS", "EU LCS", "LCK", "LPL", "LMS", "NA CHALLENGER", "EU CHALLENGER", "RIFT RIVALS", "MID-SEASON INVITATIONAL", "ALL-STAR EVENT"};      
    Select choices = new Select(Googledriver.findElement(By.className("dropdown-nav")));

    List<WebElement> dropdownvalues=choices.getOptions();   
    System.out.println(dropdownvalues.size());

    for(int i=0;i<dropdownvalues.size();i++)
    {
        Assert.assertEquals(dropdownvalues.get(i).getText(), arr[i]);
    }

1 个答案:

答案 0 :(得分:0)

您尝试分配给WebElement Class对象的Select不在select标记内。因此,它显示错误为Element should have been “select” but was “li”

虽然您没有在问题中提到确切的验证点,但我认为您正在尝试验证NA LCSEU LCS等项目。为此,我们需要循环遍历几个<a>标记,如下所示:

System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
WebDriver Googledriver = new ChromeDriver();
Googledriver.get("http://www.lolesports.com/en_US/"); 
WebDriverWait wait = new WebDriverWait(Googledriver, 10);
WebElement more_comp = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//ul[@class='main-nav-menu']/li[@class='dropdown-nav']")));
more_comp.click();
List<WebElement> listValues = Googledriver.findElements(By.xpath("//ul[@class='main-nav-menu']/li[@class='dropdown-nav']//following::ul[1]//li/a"));
for(WebElement val:listValues)
    System.out.println(val.getAttribute("innerHTML"));

这段代码块给出了一个输出:

  NA LCS

  EU LCS

  LCK

  LPL

  LMS

  NA Challenger

  EU Challenger

  Rift Rivals

  Mid-Season Invitational

  All-Star Event

现在您可以对获得的List

进行任何断言