0

I have this fragment of my HTML code:

   <a class="button-color1 full" href="/AntecipacaoRecebiveis/AntecipacaoAutomatica">Simule e contrate</a>                        </div>
                        <div class="interntab clenitab" id="tabs-2">
                            <div class="row">
                                <div class="col-xs-8 col-sm-9 taxa-mes" style="text-align:center">
                                    <span class="caption-value">a ser creditado em sua conta</span><br />
                                    <span class="value-simulator">R$ 0,00</span>
                                </div>
                                <div class="col-xs-4 col-sm-3 a-ser-creditado" style="padding-top: 24px;">
                                    <span class="caption-value" style="font-size: 13px;line-height:6px">Taxa a.m.</span><br />
                                    <span class="value-simulator" style="font-size: 20px;line-height:8px">2,50%</span>
                                </div>
                            </div>

Then, I'm trying to automate my test by clicking on the onclick="SetAdquirente('ADIQ') (I had success on many other steps from the same website), but it is generating the following error:

raise exception_class(message, screen, stacktrace)

selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document (Session info: chrome=59.0.3071.115) (Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 6.1.7601 SP1 x86_64)

My code to find it is:

    elem = driver.find_element_by_id('tabs-2')
    action.move_to_element(elem)
    action.click()
    action.perform()

Why is this happening? (I've tried to use others find_element statements, such as xpath, link text, etc and the same result raises)

Thanks a lot!

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
SnowBG
  • 89
  • 1
  • 2
  • 12
  • could you link the webpage? – 0xMH Aug 02 '17 at 18:03
  • @Mohamed unfortunately this is on development environment and has no external access. – SnowBG Aug 02 '17 at 18:26
  • Duplicate of https://stackoverflow.com/a/16244739/677518 – Ardesco May 04 '19 at 07:46
  • Possible duplicate of [Selenium WebDriver How to Resolve Stale Element Reference Exception?](https://stackoverflow.com/questions/16166261/selenium-webdriver-how-to-resolve-stale-element-reference-exception) – Ardesco May 04 '19 at 07:46

1 Answers1

0

The StaleElementException occurs when the webelement in question is changed on the dom and the initial reference to that webelement is lost.

You can search for the webelement again

try this

try:
  elem = driver.find_element_by_id('tabs-2')
  action.move_to_element(elem)
  action.click()
  action.perform()
except StaleElementReferenceException:
 elem = driver.find_element_by_id('tabs-2')
 action.move_to_element(elem)
 action.click()
 action.perform()
Satish
  • 1,976
  • 1
  • 15
  • 19