Reputation: 121
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
Upvotes: 1
Views: 250
Reputation: 13779
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)):
Upvotes: 3