1

First off, list index being out of range does sound simple and self-explanatory... but looking around I can find no explanation for my situation; I am iterating through a list 19 items long and at the 9th item my console throws the "list index out of range" error". I am at a loss for explanations quite frankly...

*I am using phantomjs and selenium to scrape a webpage... THANKS IN ADVANCE!

# data list
# -------------------------------------------------------------------------------------   
        xpath = [
        businessName,firstName,lastName,ubi,info,
        licenseType,licenseNumber,licenseEffectiveDate,licenseExpirationDate,status,
        bondProvider,bondNumber,bondAmount,bondEffectiveDate,bondEffectiveDate,insuranceProvider,
        insuranceNumber,insuranceAmount,insuranceEffectiveDate,insuranceExpirationDate
        ] 

        data = [
        "businessName","firstName","lastName","ubi","info",
        "licenseType","licenseNumber","licenseEffectiveDate","licenseExpirationDate","status",
        "bondProvider","bondNumber","bondAmount","bondEffectiveDate","bondEffectiveDate","insuranceProvider",
        "insuranceNumber","insuranceAmount","insuranceEffectiveDate","insuranceExpirationDate"
        ] 
#
#
# xpath check and grab function
# -------------------------------------------------------------------------------------
        i = 0
        while i <= len(data):
           result = browser.find_element_by_xpath(xpath[i]).text    #checks is xpath exists
           print i
           print data[i] + " = " + str(result) 
           i += 1
6
  • 1
    What is i <= data[i] supposed to do? Commented Feb 22, 2016 at 4:37
  • 4
    Your while loop will definitely go out of bounds, are you sure the error occurs on the 9th iteration? Try changing while (i <= data[i]) to while i <= len(data): Commented Feb 22, 2016 at 4:37
  • you're much better to use for i in range(len(xpath)-1 rather than a while loop. Commented Feb 22, 2016 at 4:38
  • I'm also mystified by i <= data[i]. Guess it's a good thing that this becomes a TypeError in python 3. Commented Feb 22, 2016 at 4:52
  • @JacobH I have update to what you suggested about and am still getting an erron on the 9th iteration Commented Feb 22, 2016 at 5:09

1 Answer 1

3

If, as it appears from your example, the items in xpath and data will always correspond directly to each other, you could do this more easily and cleanly by using the loop expression:

for elem_xpath, name in zip(xpath, data):

If you need the index i as well, use enumerate:

for i, (elem_xpath, name) in enumerate(zip(xpath, data)):
Sign up to request clarification or add additional context in comments.

1 Comment

This is very helpful and I will use it... but I have just discovered that commenting out the following line gets me past the 9th iteration: "result = browser.find_element_by_xpath(xpath[i]).text"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.