如何创建List <webelement>并单击引发java.lang.IndexOutOfBoundsException的选项

时间:2018-07-19 01:13:05

标签: java selenium selenium-webdriver xpath indexoutofboundsexception

我正在测试xpath是动态的网站,并寻找一种方法来单击列表中的各种链接。我决定尝试在ul标签处创建一个静态的webelements列表。但是,当我这样做时,会出现如下所示的异常:

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();

2 个答案:

答案 0 :(得分:0)

发生索引越界异常是因为获得的WebElement列表的大小等于1。我的意思是,您想检索包含链接Biomedical and Health Science的<LI>元素。但是,代码段driver.findElements(By.xpath("//*[@id=\"subjects-list\"]"))的ID为<UL>的{​​{1}}元素在页面中是唯一的。因此,列表的大小为1,调用subject-list时会引发异常。

总结起来,要使其起作用,您应该做类似的事情:

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>

答案 1 :(得分:0)

要为所有可用的主题创建列表,您需要为元素的可见性引入 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;
                }
        }
    }
    
  • 浏览器快照:

Dana Fisher