All -
For a Selenium webscraper using Python 3.x - I am trying to get a printout that depends upon the length of each 1d index in a jagged list, in this case only 2d. The list is named masterViewsList
, and the lists it contains are versions of a list named viewsList
. Below see how my list masterViewsList
of viewsList
's is constructed:
from selenium import webdriver
import os
masterLinkArray = []
masterViewsList = []
# a bunch of code here that I cut out for simplicity's sake
for y in range(0, len(masterLinkArray)):
browser = webdriver.Chrome(chromePath)
viewsList = []
browser.get(masterLinkArray[y])
productViews = browser.find_elements_by_xpath("// *[ @ id = 'lightSlider'] / li / img")
counter = - 1
for a in productViews:
counter = counter + 1
viewsList.append(a.get_attribute('src'))
print(viewsList[counter])
print(len(viewsList))
masterViewsList.append(viewsList)
if y == 10:
print(masterViewsList[y])
print(len(masterViewsList[y]))
del viewsList[:]
print(len(masterLinkArray))
print(len(masterViewsList))
print(len(masterViewsList[0]))
print(len(masterViewsList[1]))
print(len(masterViewsList[10]))
The printout is this:
["https://us.testcompany.com/images/is/image/lv/1/PP_VP_L/544_PM2_Front%20view.jpg?wid=140&hei=140","https://us.testcompany.com/images/is/image/lv/1/PP_VP_L/544_PM1_Side%20view.jpg?wid=140&hei=140","https://us.testcompany.com/images/is/image/lv/1/PP_VP_L/544_PM1_Interior%20view.jpg?wid=140&hei=140","https://us.testcompany.com/images/is/image/lv/1/PP_VP_L/544_PM1_Other%20view.jpg?wid=140&hei=140","https://us.testcompany.com/images/is/image/lv/1/PP_VP_L/544_PM1_Other%20view2.jpg?wid=140&hei=140"]
5
79
79
0
0
0
As you can see, neither the masterLinkArray
, nor the masterViewsList
are empty - they're 79 long. Also, print(masterViewsList[y])
prints out an actual non-empty list, one with a recognized length of 5. Oddly, once I leave the for y
loop, len(masterViewsList[*any integer*])
prints out to "0". These similar questions:
Find the dimensions of a multidimensional Python array,
Find length of 2D array Python,
both indicate that len(array[*integer*])
is the proper way to get the length of a list within a list, but in my case this appears not to be working consistently.