如何创建一个列表<WebElement> 并点击一个抛出 java.lang.IndexOutOfBoundsException 的选项

How to create a List<WebElement> and click on an option which is throwing java.lang.IndexOutOfBoundsException

我正在测试一个 xpath 是动态的网站,并寻找一种方法来点击列表中的各种链接。我决定尝试在静态的 ul 标签上创建一个网络元素列表。但是,当我这样做时,我得到了如下所示的越界异常:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 
2, Size: 1
at java.util.ArrayList.rangeCheck(ArrayList.java:657)
at java.util.ArrayList.get(ArrayList.java:433)
at automationFramework.ThirdIronTest.main(ThirdIronTest.java:40)

我知道 li 元素的大小超过 1,我在搜索每个元素之前隐式等待以确保加载每个页面,但它似乎仍然无法正常工作。我错过了什么?

这是我的代码:

public static void main(String[] args) throws InterruptedException, IOException {

    // Open Chrome browser
    WebDriver driver = new ChromeDriver();

    // Maximize browser window
    driver.manage().window().maximize();

    // Navigate to QA Environment to begin test
    driver.get("https://qa-safari-develop.browzine.com/libraries/14/subjects");

    // Allow search for elements to wait for page(s) to load
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

    //** HERE WE ARE CREATING ELEMNTS OF SUBJECT LIST
    List<WebElement> subjectElems = driver.findElements(By.xpath("//*[@id=\"subjects-list\"]"));

    // Click on the subject Biomedical and Health Sciences from the Browse Subjects Navigation
    subjectElems.get(2).click();

发生索引越界异常是因为您正在获取大小等于 1 的 WebElements 列表。我的意思是,您想要检索包含 link 生物医学和健康科学的 <LI> 元素。然而,这段代码 driver.findElements(By.xpath("//*[@id=\"subjects-list\"]")) 得到了 id subject-list<UL> 元素,它在页面中是唯一的。因此,列表的大小为 1,当您调用 subjectElems.get(2).click().

时会抛出异常

总而言之,要使其有效,您应该执行类似于以下操作的操作:

// Navigate to QA Environment to begin test
driver.get("https://qa-safari-develop.browzine.com/libraries/14/subjects");

// Allow search for elements to wait for page(s) to load
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

//** Getting the ul with the links
WebElement subjectElems = driver.findElement(By.xpath("//*[@id=\"subjects-list\"]"));

  // looking for an element with link text = Biomedical and Health Sciences
   WebElement biomedicalAndHealthSciences = subjectElems.findElement(By.linkText("Biomedical and Health Sciences"));

System.out.println(biomedicalAndHealthSciences.getText());
// Click on the subject Biomedical and Health Sciences from the Browse Subjects Navigation
biomedicalAndHealthSciences.click();

如果您想遍历 <UL> 中的所有 link:

    WebElement subjectElems = driver.findElement(By.xpath("//*[@id=\"subjects-list\"]"));


 List<WebElement> linkList =  subjectElems.findElements(By.tagName("a"));

       for(WebElement link: linkList) {
             System.out.println(link.getText());
             link.click();
       }

要创建一个包含所有可用 SubjectsList,您需要引入 WebDriverWait 元素的可见性,然后单击您选择的主题,例如生物医学与健康科学,您可以使用以下解决方案:

  • 代码块:

    import java.util.List;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class click_list_item {
    
        public static void main(String[] args) {
    
            System.setProperty("webdriver.gecko.driver", "C:\Utility\BrowserDrivers\geckodriver.exe");
            WebDriver driver = new FirefoxDriver();
            driver.get("https://qa-safari-develop.browzine.com/libraries/14/subjects");
            List<WebElement> myElements = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//a[@class='subjects-list-subject ember-view']/span[@class='subjects-list-subject-name']")));
            for(WebElement elem:myElements)
                if(elem.getAttribute("innerHTML").contains("Biomedical and Health Sciences"))
                {
                    elem.click();
                    break;
                }
        }
    }
    
  • 浏览器快照: