0

I have a for loop that itterates over a list of URLS. The urls are then loaded by the chrome driver. Some urls will load a page that is in a 'bad' format and it will fail the first xpath test. If it does I want it to go back to the next element in the loop. My cleanup code works but I can't seem to get it to go to next element in the for loop. I have an except that closes my websbrowser but nothing I tried would all me to then loop back to 'for row in mysql_cats'

for row in mysql_cats : 
   print ('Here is the url -', row[1])
   cat_url=(row[1])
   driver = webdriver.Chrome()
   driver.get(cat_url); #Download the URL passed from mysql

   try:   
      CategoryName= driver.find_element_by_xpath('//h1[@class="categoryL3"]|//h1[@class="categoryL4"]').text   #finds either L3 or L4 catagory 
   except:
      driver.close()
      #this does close the webriver okay if it can't find the xpath, but not I cant get code here to get it to go to the next row in mysql_cats
personalt
  • 810
  • 3
  • 13
  • 26
  • The way your code written, it should be doing exactly what you described, except that you should not start a new driver at each iteration . – DYZ Jun 24 '18 at 20:21

1 Answers1

2

I hope that you're closing the driver at the end of this code also if no exceptions occurs.
If you want to start from the beginning of the loop when an exception is raised, you may add continue, as suggested in other answers:

try:   
    CategoryName=driver.find_element_by_xpath('//h1[@class="categoryL3"]|//h1[@class="categoryL4"]').text   #finds either L3 or L4 catagory 
except NoSuchElementException:
    driver.close()
    continue # jumps at the beginning of the for loop

since I do not know your code, the following tip may be useless, but a common way to handle this cases is a try/except/finally clause:

for row in mysql_cats : 
    print ('Here is the url -', row[1])
    cat_url=(row[1])
    driver = webdriver.Chrome()
    driver.get(cat_url); #Download the URL passed from mysql
    try:
        # my code, with dangerous stuff
    except NoSuchElementException:
        # handling of 'NoSuchElementException'. no need to 'continue'
    except SomeOtherUglyException:
        # handling of 'SomeOtherUglyException'
    finally: # Code that is ALWAYS executed, with or without exceptions
        driver.close()

I'm also assuming that you're creating new drivers each time for a reason. If it is not voluntary, you may use something like this:

driver = webdriver.Chrome()
for row in mysql_cats : 
    print ('Here is the url -', row[1])
    cat_url=(row[1])
    driver.get(cat_url); #Download the URL passed from mysql
    try:
        # my code, with dangerous stuff
    except NoSuchElementException:
        # handling of 'NoSuchElementException'. no need to 'continue'
    except SomeOtherUglyException:
        # handling of 'SomeOtherUglyException'
driver.close()

In this way, you have only one driver that manages all the pages you're trying to open in the for loop

have a look somewhere about how the try/except/finally is really useful when handling connections and drivers.
As a foot note, I'd like you to notice how in the code I always specify which exception I am expecting: catching all the exception can be dangerous. BTW, probably no one will die if you simply use except:

Gsk
  • 2,929
  • 5
  • 22
  • 29
  • I was closing and recreating the driver with each loop because is another similar project if I didnt do this memory usage would just continue to grow. I didnt need to really restart the browser after each loop but it was tough to judge how quickly the memory would overflow so it was easier to just use that as a place to restart the brower – personalt Jun 25 '18 at 00:22
  • Ok. Were you able to solve your problem? Is One of the solutions working for you? – Gsk Jun 25 '18 at 06:08
  • yout answer worked like a champ and was very helpful. I marked it as best answer. – personalt Jun 25 '18 at 23:10