-2

I'm new to Selenium automation. Keeping that in mind please do answer to the question below, even if it sounds absurd to techies out there.

I'm trying to get all the id in a page: (I'm using Python and new to this as well)

Here's the code I used:

for b1 in driver.find_elements_by_xpath('//*[@id]'):
            print b1.get_attribute('id')

While this displays the ids as expected, at one point the script ends up with the 'StaleElementReferenceException'.

I catch this exception:

try:
        for b1 in driver.find_elements_by_xpath('//*[@id]'):
            print b1.get_attribute('id')
except StaleElementReferenceException:
        print 'Exception'

Now, though the exception is caught, my script stops there without displaying all the ids.

It's displaying all the ids until this exception point and then stops there.

How can I make this continue doing what it does and just skip this exception?

depperm
  • 10,606
  • 4
  • 43
  • 67
Krishnakumar
  • 725
  • 1
  • 6
  • 11
  • use the `pass` command - duplicate of http://stackoverflow.com/questions/730764/try-except-in-python-how-do-you-properly-ignore-exceptions – Razzildinho Jan 20 '17 at 15:39

2 Answers2

0

Ignoring the exception might lead to missing id attribute values.

The exception itself is most likely the result of the page being not fully loaded at the time you've located all the elements with an id. Give the page more time to load and then find the elements.

You can first try to increase the page load timeout with .set_page_load_timeout(). Or, a more reliable way to do it might be your webpage-specific - use WebDriverWait to wait for presence/visibility of a certain element on a page which may be an indication that the page has been loaded. For example, if your page loads a list of products asynchronously, you can wait for the visibility of the first product element/block/part of the page.

If the page is continuously loading and updating the DOM tree causing the stale element exceptions, you can force stop the page load.


As a side note, if all you need is to get the id attributes and performance really matters, doing it with selenium only has a lot of overheads - every single get_attribute() command in the loop is a JSON over HTTP command which is costly (related topic, if interested). Instead, you can wait for the page to load, grab the driver.page_source and use an HTML parser, like lxml or BeautifulSoup to extract the id attribute values.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • getting the page source and using BeautifulSoup, worked like a GEM... and paved an easy way to achieve what I wanted... Thanks a lot @alecxe – Krishnakumar Jan 24 '17 at 07:56
  • @Krishnakumar awesome, glad to help, see if we can "close" the topic by having this answer marked as accepted. Thanks. – alecxe Jan 24 '17 at 14:28
0

You have to use the pass keyword in your exception handling :

 except StaleElementReferenceException:
    pass
Setoh
  • 88
  • 5
  • It's the same result again. My first Script>> `try: for b1 in driver.find_elements_by_xpath('//*[@id]'): print b1.get_attribute('id') except StaleElementReferenceException: print 'Exception'` Result end as this: itemInfo_I2HGJ47IG0J36B itemName_I2HGJ47IG0J36B Exception I change the script: `try: for b1 in driver.find_elements_by_xpath('//*[@id]'): print b1.get_attribute('id') except StaleElementReferenceException: pass` Result: itemInfo_I2HGJ47IG0J36B itemName_I2HGJ47IG0J36B Once again it ends at the same point and doesn't move ahead with printing the other ids – Krishnakumar Jan 23 '17 at 07:04