Selenium pit (StaleElementReferenceException:Message:Element not found in the cache ... )

Source: Internet
Author: User

Today, we share a pit that is often met in a selenium:

Selenium.common.exceptions.StaleElementReferenceException:Message:Element not found in the Cache-perhaps the page has Changed since it is looked up

People in the group often ask, "I loop to click on a column of links, but only to the first one, the second one failed, why?" ”。 Here's why: when you click on the second one, you're already a new page, and of course you can't find the elements of the previous page. at this time, he will ask "but the elements are there, there is no change, even I was back back, the page has not changed, how can say is the new page?" ”。 I need you to understand that. The page is the same size does not mean that the same page, just as two people are not necessarily the same person, their identity card number is different. page, even the elements on the page have their own ID (ID).

Let's try it out:

Code:

#-*-coding:utf-8-*- fromSeleniumImportWebdriverdriver=Webdriver. Firefox () Driver.get ('http://www.baidu.com')PrintDRIVER.FIND_ELEMENT_BY_ID ('kw')#kw before RefreshDriver.refresh ()#RefreshPrintDRIVER.FIND_ELEMENT_BY_ID ('kw')#kw after Refreshdriver.quit ()

Results:

<selenium.webdriver.remote.webelement.webelement (session="6c251157-6d81-435c-9100-97696a46ab9c  ", element="{f74ae41d-a557-4d5c-9029-3c122e3d0744}") >< Selenium.webdriver.remote.webelement.WebElement (session="6c251157-6d81-435c-9100-97696a46ab9c  ", element="{d7bd4320-31f2-4708-824f-f1a8dba3e79b}") >

We found that just to refresh the page, two times the element ID is different, that is to say this is two different elements , if you use the following way to locate, naturally because can not find the error:

#-*-coding:utf-8-*- fromSeleniumImportWebdriverdriver=Webdriver. Firefox () Driver.get ('http://www.baidu.com') kw= driver.find_element_by_id ('kw')#locate and obtain the KW element firstKw.click () Driver.refresh ( )#RefreshKw.click ()#after the refresh, still use the original KW element operation, this will be an errordriver.quit ()

Results:

Traceback (most recent): File"d:/code/py/autotestframework/src/others/tryrefreshpage.py", line 16,inch<module>Kw.click () File"C:\APP\Python2.7.10\lib\site-packages\selenium\webdriver\remote\webelement.py", Line 75,inchClick Self._execute (command.click_element) File"C:\APP\Python2.7.10\lib\site-packages\selenium\webdriver\remote\webelement.py", Line 469,inch_executereturnself._parent.execute (command, params) File"C:\APP\Python2.7.10\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 201,inchExecute Self.error_handler.check_response (response) File"C:\APP\Python2.7.10\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194,inchCheck_responseRaiseexception_class (message, screen, StackTrace) Selenium.common.exceptions.StaleElementReferenceException: Message:element notFoundinchThe Cache-Perhaps the page has changed since it is looked upstacktrace: ....

The reason is obvious, you use other people's ID card to find the current person, even if these two people look like, he will tell you: Sorry, you have the wrong person .

Of course, not only this situation, if you do the following actions, it is possible to find the wrong person:

    • Refresh, whether you are actively refreshing or the page is automatically refreshed
    • Back, has jumped to the other pages, and then you use Driver.back (), this is a new page
    • Jump to a new page, but there are some elements on this new page that look like the previous page, and this is a new page. For example: A row of paging buttons, you click on the next page to jump to the second page, want to also use the original element to the next page, it is not possible.
      There may be other reasons, in short you see this type is similar, but the page has the operation of the situation, you should think about this possibility.

What do you do when you encounter this situation?

Very simple:

Just refresh the page after retrieving the elements on the line, do not get a set of elements in advance, and then go through the loop operation of each element, this situation is to get the number of elements, and then in the loop to get the corresponding position of the element, in the use of the time to get, so you get to the latest ID, there will not be the wrong

in a word, encounter the situation of the page changes, do not go round the elements, to the number of loops or positioning methods, in the loop to get elements.

Selenium Pit (StaleElementReferenceException:Message:Element not found in the cache ... )

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.