Carol.Kar
Carol.Kar

Reputation: 5355

IndexOutOfBoundException - retrieve all elements from a site

I have coded the following method to return me an object of all elements on a website.

public ArrayList<Person> getWantedFields() {
    log.info("retrieve wanted fields");

    resultList = new ArrayList<Person>();

    List<WebElement> fullNames   = driver.findElements(By.xpath("//div[@id='content_head_folge']/h1"));
    List<WebElement> professions = driver.findElements(By.xpath("//div[@id='content_head_folge']/p"));
    List<WebElement> streets     = driver.findElements(By.xpath("//div[@id='content_head_folge']/p/br[1]"));

    //2811 results
    for (int i = 0; i < 2810; i++) {
        resultList.add(new Person(fullNames.get(i).getText(), professions.get(i).getText(), streets.get(i).getText(), null, null, null, null, null));  //here I get the OutOfBounds exception                   
    }
    log.info(resultList.toString());

    return resultList;
}

I guess the 2811 displayed results are too much. Is there a way to iterate over all fields which are on the site, without knowing its size?

I appreciate your reply!

UPDATE

Using

public ArrayList<Person> getWantedFields() {
    log.info("retrieve wanted fields");

    resultList = new ArrayList<Person>();

    List<WebElement> fullNames   = driver.findElements(By.xpath("//div[@id='content_head_folge']/h1"));
    List<WebElement> professions = driver.findElements(By.xpath("//div[@id='content_head_folge']/p"));
    List<WebElement> streets     = driver.findElements(By.xpath("//div[@id='content_head_folge']/p/br[1]"));

    //2811 results
    for (int i = 0; i < fullNames.size(); i++) {
        resultList.add(new Person(fullNames.get(i).getText(), professions.get(i).getText(), streets.get(i).getText(), null, null, null, null, null));  //here I get the OutOfBounds exception                   
    }
    log.info(resultList.toString());

    return resultList;
}

I get:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.rangeCheck(ArrayList.java:635)
    at java.util.ArrayList.get(ArrayList.java:411)
    at com.Test.service.PM.getFields(PM.java:152)
    at com.Scraponator.gui.PersonLookup.main(PersonLookup.java:36)

Upvotes: 0

Views: 131

Answers (2)

laune
laune

Reputation: 31300

You are walking on thin ice here. How can you be sure that all three lists have the same size? What if there are gaps (missing sub-elements)?

List<WebElement> fullNames   = driver.findElements(By.xpath("//div[@id='content_head_folge']/h1"));
List<WebElement> professions = driver.findElements(By.xpath("//div[@id='content_head_folge']/p"));
List<WebElement> streets     = driver.findElements(By.xpath("//div[@id='content_head_folge']/p/br[1]"));

if( fullNames.size() != professions.size() || fullNames.size() != streets.size() ){
    throw new IllegalStateException( "can't get the data" );
}

Upvotes: 1

Richard
Richard

Reputation: 9029

You can use the size() of the list:

for (int i = 0; i < fullNames.size(); i++) {
    resultList.add(new Person(fullNames.get(i).getText(), professions.get(i).getText(), streets.get(i).getText(), null, null, null, null, null));  //here I get the nullpointer exception                   
}

Upvotes: 1

Related Questions