Skip to content
This repository has been archived by the owner. It is now read-only.

Python bindings: EventFiringWebDriver can suppress StaleElementReferenceException in some cases #7739

Closed
lukeis opened this issue Mar 4, 2016 · 3 comments

Comments

@lukeis
Copy link
Member

@lukeis lukeis commented Mar 4, 2016

Originally reported on Google Code with ID 7739

Look at the following code

import time
import unittest
from selenium.common.exceptions import StaleElementReferenceException
from selenium.webdriver.firefox.webdriver import WebDriver
from selenium.webdriver.support.abstract_event_listener import AbstractEventListener
from selenium.webdriver.support.event_firing_webdriver import EventFiringWebDriver


class DemoBug(unittest.TestCase):
    def setUp(self):
        # With not wrapped driver try/except block works as expected.
        #self.driver = WebDriver()

        # With wrapped driver StaleElementReferenceException is suppressed
        # and AttributeError is thrown instead.
        # So, try/except block doesn't catch proper exception.
        self.driver = EventFiringWebDriver(WebDriver(), AbstractEventListener())

    def tearDown(self):
        self.driver.quit()

    def test_demo_bug(self):
        driver = self.driver
        driver.get('http://google.com')
        query_field = driver.find_element_by_name('q')
        query_field.send_keys('selenium')
        time.sleep(1)
        first_result_link = driver.find_elements_by_css_selector('li.g a')[0]
        driver.find_element_by_css_selector('a#pnnext').click()
        time.sleep(1)
        driver.find_element_by_css_selector('a#pnprev').click()
        time.sleep(1)
        try:
            print first_result_link.text
        except StaleElementReferenceException:
            first_result_link = driver.find_elements_by_css_selector('li.g a')[0]
            print first_result_link.text


What is the expected output? What do you see instead?
Expected: StaleElementReferenceException is thrown in try/except block.
Actual: AttributeError is thrown in try/except block.


Selenium version: selenium==2.42.1
OS: Ubuntu
Browser: Any
Browser version: Any


The described behavior is caused by using 'hasattr' instruction in __getattr__ method.
So, if you remove this all will work as expected.

Reported by imboot85 on 2014-08-12 15:35:08

@lukeis

This comment has been minimized.

Copy link
Member Author

@lukeis lukeis commented Mar 4, 2016

Reported by barancev on 2014-08-18 08:49:49

  • Labels added: Lang-Python
@lukeis

This comment has been minimized.

Copy link
Member Author

@lukeis lukeis commented Mar 4, 2016

the attribute error is being thrown on the first "print first_result_link.text"

because the EventFiringWebElement does not implement .text

you have to use the wrapped element:

try:
  print first_result_link.wrapped_element.text
except StaleElementReferenceException:
  first_result_link = driver.find_elements_by_css_selector('li.g a')[0]
  print first_result_link.wrapped_element.text


closing this issue for now... since this never seemed to implement .text, but if we
wanted the event firing web element to be a drop in replacement for existing tests,
then we should expose things like .text and get_attribute (without dispatching events).
Please open a new issue if that is desired.

Reported by luke.semerau on 2014-10-11 04:37:25

  • Status changed: WorkingAsIntended
@lukeis

This comment has been minimized.

Copy link
Member Author

@lukeis lukeis commented Mar 4, 2016

Reported by luke.semerau on 2015-09-17 18:24:19

  • Labels added: Restrict-AddIssueComment-Commit
@lukeis lukeis closed this Mar 4, 2016
@SeleniumHQ SeleniumHQ locked and limited conversation to collaborators Mar 4, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.