1

My previous question was Selenium Python - Access next pages of search results

After successfully doing this for one keyword, i need to read these keywords line by line from a file, and keep on searching.

file1 = open("M:/Users/Cyborg/Desktop/Py/read.txt","r")

the url is http://www.nice.org.uk/

driver.switch_to_default_content()
str2 = file1.readline()
inputElement = driver.find_element_by_name("searchText")
inputElement.clear()
inputElement.send_keys(str2)

this reads the keyword to be searched from the file, then enters it in the search box.

inputElement.submit()

submits the query. It's pretty much the same as in the question I asked before, and everything works perfectly if str2 is static, that is I define str2='abc' instead of reading from file. However as soon as I try to read from file and continue searching, I get this error

File "M:\Users\Cyborg\Desktop\Py\test.py", line 22, in <module>
    inputElement.submit()
  .
  .
  .
  .

 raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: 'Element not found in the cache - perhaps the page has changed since it was looked up' ;

For some reason I get this error.

Any help would be greatly appreciated!

Community
  • 1
  • 1
user3691767
  • 115
  • 1
  • 5
  • 11
  • Are you entering values, submitting, then trying to enter more values, and submit again, still using `inputElement`? – Richard Jun 11 '14 at 22:37
  • Yes. What I need to do is enter value, submit, go through the search results, then enter another value, submit and repeat – user3691767 Jun 11 '14 at 22:43

1 Answers1

0

Your issue is that after you perform inputElement.submit() once, the page is likely being refreshed, and at that point inputElement becomes stale.

I think getting a new reference for inputElement after each inputElement.submit() will clear it up:

inputElement.submit()
inputElement = driver.find_element_by_name("searchText")
Richard
  • 8,961
  • 3
  • 38
  • 47
  • I think i got it to work. Instead of after submit(), i referenced inputElement again after send_keys(). Thanks a lot! – user3691767 Jun 11 '14 at 23:14