2

I have a python script that uses selenium. The steps of the script is:

  1. Login
  2. Go to a page which starts a file harvester process
  3. After every 1 minute refresh the status page to check if the file harvester has completed - which is indicated in a table on the page

The problem that I am having is that when the page is refreshed using browser.refresh() I get the following error

Traceback (most recent call last):
  File "D:\ScheduledTasks\Scripts\ScriptArchive\COL_INSPIRE\INSPIRE_METADATA_v1.1.py", line 491, in <module>
    print head.text
  File "C:\Python27\ArcGIS10.2\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\remote\webelement.py", line 61, in text
    return self._execute(Command.GET_ELEMENT_TEXT)['value']
  File "C:\Python27\ArcGIS10.2\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\remote\webelement.py", line 385, in _execute
    return self._parent.execute(command, params)
  File "C:\Python27\ArcGIS10.2\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 173, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\ArcGIS10.2\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 166, in check_response
    raise exception_class(message, screen, stacktrace)
StaleElementReferenceException: Message: Element not found in the cache - perhaps the page has changed since it was looked up

This used to work, but now the above error is appearing, what is the best way to to "poll" a page to check whether a piece of text has changed.

My code is

header = browser.find_elements(By.TAG_NAME,"tr")
#go through each header to get teh one we want
headerIndex = 0
for head in header:

 #print headerIndex
 print head.text
 if "Next harvest" in head.text:
    #Get the table data for the header that we want
    tdata = header[headerIndex].find_elements(By.TAG_NAME,"td")
    for t in tdata:
       print t.text
       if "Scheduled" in t.text:
          #wait 60 seconds
          time.sleep(60)
          browser.refresh()
       elif "Not yet scheduled" in t.text:
          refreshComplete = True
          break
 if refreshComplete == True:
    break
 headerIndex = headerIndex + 1

1 Answer 1

2

You have to find the headers every time you refresh the page.

I would also switching to using find_element(s)_by_xpath to check the text:

while True:
    header = browser.find_element_by_xpath('//tr[contains(., "Next harvest")]')

    not_scheduled = header.find_elements_by_xpath('//td[contains(., "Not yet scheduled")]')
    if not_scheduled:
        break

    time.sleep(60)
    browser.refresh()

Hope you've got the idea behind the solution (but check if I've followed the logic correctly).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, using find_element_by_xpath worked and your code is a lot cleaner than mine, So thanks!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.