-2

I am running a bunch of code all at once in python by copying it from my editor and pasting it into python. This code includes nested for loops. I am doing some web scraping and the program quits at different times. I suspect that this is because it doesn't have time to load. I get the following error (once again - the program scrapes different amounts of text each time):

Traceback (most recent call last):
  File "<stdin>", line 35, in <module>
IndexError: list index out of range

First, what does line 35 refer to? Is this the place in the relevant inner for-loop?

Second, I think that the error might be caused by a line of code using selenium like this:

driver.find_elements_by_class_name("button")[j-1].click()

In this case, how can handle this error? What is some example code with either explicit waits or exception handling that would address the issue?

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
bill999
  • 2,147
  • 8
  • 51
  • 103
  • 5
    Normally, for anything more than very small snippets, you would save your Python script into a file and then run it through the `python` command line command. This will be far more sustainable than pasting into a Python REPL. The benefit, in your current situation, is that the line number reported by Python will refer to an actual line number in your file. – Greg Hewgill Nov 19 '14 at 22:35
  • Is there any advantage to using the `python` method vs. `execfile(filename)` in the Python REPL? – bill999 Nov 19 '14 at 22:46
  • 1
    I suppose you could use `execfile()`, that would work equally well. – Greg Hewgill Nov 19 '14 at 23:02
  • 1
    It was the 35th line pasted. Pasting a script can fail if one defined function calls another later in the posted file. But you get an error about a name not being defined. Why not just write the script to a file and test that? – tdelaney Nov 19 '14 at 23:04

3 Answers3

2

It means that [j-1] doesn't exist for a given value of j, possibly if j-1 exceeds the max number of elements in the list

AtAFork
  • 376
  • 3
  • 9
1

You can try your code and catch an IndexError exception like this:

try:
    # your code here
except IndexError:
    # handle the error here

An IndexError happens when you try to access an index of a list that does not exist. For example:

>>> a = [1, 2, 3]
>>> print(a[10])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

It's difficult to say how you should handle the error without more detail.

PhilG
  • 109
  • 2
1

When working with code snippets, it's convenient to have them open in a text editor and either

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152