Help with stale exception Python

dieter dieter at handshake.de
Tue Dec 8 02:40:27 EST 2015


iverson.zhou at gmail.com writes:
> ...
> I have tried uncountable number of methods (e.g. explicit, implicit wait) but the stale error still persists as it seems to stays stale as long as it is staled.
>
> Have anyone come up with a solution to this and what is the best way to deal with DOM tree changes.
> ...
> with open('C:/Python34/email.csv','w') as f:
> z=csv.writer(f, delimiter='\t',lineterminator = '\n',)
> while True:
>         row = []
>         for link in driver.find_elements_by_xpath("//*[@id='wrapper']/div[2]/div[2]/div/div[2]/div[1]/div[3]/div[1]/div[2]/div/div[2]/div/div[2]/div/div[position() = 1 or position() = 2 or position() = 3]"):

The "find_elements_by_xpath" likely gives you a list of elements
on the *initial* page. The "for" sets things up that this list
is iterated over.

>             try:
> ...
>                 c=driver.find_element_by_id('detail-pagination-next-btn')
> ...
>                 c.click()
> ...
>             except StaleElementReferenceException as e:
This "click" may change the page which means that
you would now be on a page different from the initial page.

It is likely that your web access framework contains some form
of automatic garbage collection: when you switch to a new page,
references to the old page may become stale.

This could explain that you sometimes observe a
"StaleElementReferenceException".


Read the documentation of your web access framework to find
out whether you can control stalelifying of elements when you
switch a page.

If this is impossible, avoid accessing elements of a page
once you have switched to a new page. To this end, extract (and store)
all relevant information from the page before you switch to a new one
and, if necessary, use the extracted information to later restore
the previous page state.


As you can see from my explanation: your question is much more
related to your web access framework than to Python in general.

Likely, there is a forum (dedicated to this framework)
that can better help you with this question than this general
Python list.




More information about the Python-list mailing list