Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: dieter Newsgroups: comp.lang.python Subject: Re: Help with stale exception Python Date: Tue, 08 Dec 2015 08:40:27 +0100 Lines: 52 Message-ID: References: <99840119-b7c2-4171-b2f8-7c600c85bd87@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: news.uni-berlin.de ++x1uS4o8FKGQl4UPpq1vAY2UWyTTQYIFms9NiduDEIw== Cancel-Lock: sha1:sNKpOdZtn/f1H7JyBoKMT8EmxXw= Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'extracted': 0.07; 'garbage': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'stale': 0.09; 'subject:Help': 0.10; 'python': 0.10; '"for"': 0.16; 'general.': 0.16; 'iterated': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'row': 0.16; 'skip:d 140': 0.16; 'subject:exception': 0.16; 'true:': 0.16; 'later': 0.16; 'switched': 0.18; 'tree': 0.18; 'try:': 0.18; 'skip:" 30': 0.20; 'implicit': 0.22; 'seems': 0.23; 'elements': 0.23; 'references': 0.23; 'sets': 0.23; 'tried': 0.24; 'header :User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'skip:" 20': 0.26; 'error': 0.27; '(e.g.': 0.27; 'switch': 0.27; 'question': 0.27; 'page.': 0.28; 'initial': 0.28; 'dom': 0.29; 'end,': 0.29; 'necessary,': 0.29; 'skip:s 30': 0.31; 'anyone': 0.32; 'related': 0.32; 'extract': 0.33; 'except': 0.34; 'previous': 0.34; 'skip:d 20': 0.34; 'list': 0.34; 'gives': 0.35; 'could': 0.35; 'sometimes': 0.35; 'but': 0.36; 'there': 0.36; '(and': 0.36; 'framework': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'charset:us-ascii': 0.37; 'list.': 0.37; 'things': 0.38; 'skip:z 10': 0.38; 'means': 0.39; 'to:addr:python.org': 0.40; 'subject:with': 0.40; 'still': 0.40; 'received:de': 0.40; 'some': 0.40; 'your': 0.60; 'avoid': 0.61; 'email addr:gmail.com': 0.62; 'more': 0.63; 'different': 0.63; 'information': 0.63; 'received:217': 0.66; 'restore': 0.70; 'state.': 0.72; 'impossible,': 0.84; 'explanation:': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: pd9e09fef.dip0.t-ipconnect.de User-Agent: Gnus/5.1008 (Gnus v5.10.8) XEmacs/21.4.22 (linux) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:100138 iverson.zhou@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.