有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java元素本应为“select”,但为“li”

我试图让selenium进入下拉列表,并验证下拉列表中的每个选项。我尝试过使用Xpath,但仍然没有成功。此外,如果有一种方法可以在不硬编码的情况下清理阵列,那么所有选项都将非常好。以下是我的代码:

    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) 个答案

  1. # 1 楼答案

    试图分配给Select类对象的WebElement不在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执行任何断言