splinter icon indicating copy to clipboard operation
splinter copied to clipboard

splinter.driver.webdriver.WebDriverElement.screenshot: "'WebDriverElement' object has no attribute "recover_screen""

Open DH-MP opened this issue 6 years ago • 2 comments

When using screenshot function under splinter.driver.webdriver.WebDriverElement I get the following error:

'''

    def screenshot(self, name='', suffix=".png", full=False):
        name = name or ''
        (fd, filename) = tempfile.mkstemp(prefix=name, suffix=suffix)
        # don't hold the file
        os.close(fd)

        if full:
            self.parent.full_screen()
        target = self.screenshot_as_png()
       > self.parent.recover_screen()
       E       AttributeError: 'WebDriverElement' object has no attribute "recover_screen"

'''

I believe it's because of the refactored function "_find" in .../Webdriver/init.py. When WebDriverElement calls the "_find" function, it instantiates the object using "self" on line 254

def _find(self, finder, selector):

     [...]

    if elements:
        elem_list = [self.element_class(element, self) for element in elements]

    return elem_list

and self, type WebDriverElement, is passed to become the parent of that element. And that's when the error occurs because the screenshot function expects self.parent to be of type WebDriver that has 'recover_screen' function.

This was working before because parent was always type "WebDriver". This can be seen in find_by methods in WebDriverElement. For example, looking at older code (ver 11), you can see that 'WebDriverElement.find_by_xpath' passes the parent instead of self to instantiate.

def find_by_xpath(self, selector, original_find=None, original_query=None):
    elements = ElementList(self._element.find_elements_by_xpath(selector))
    return ElementList(
        [self.__class__(element, self.parent) for element in elements],
        find_by="xpath",
        query=selector,
    )

Quick solution is to pass the parent instead of self in "_find" function when self is of type WebDriverElement but I do not know the code enough and may cause more issues.

Another possible solution is to implement the the missing functions that was expected for "WebDriver" such as "recover_screen", "full_screen" for WebDriverElement class.

Let me know if any of the solution above is good and I'll create a pull request for it.

DH-MP avatar Dec 18 '19 06:12 DH-MP

@DH-MP Thanks for the detailed bug report.

I think the easiest fix here is to add self.parent = self to BaseWebDriver, and change elem_list = [self.element_class(element, self) for element in elements] to elem_list = [self.element_class(element, self.parent) for element in elements] in _find().

This bug has revealed some blind spots in the unit tests, so hopefully we can get some new tests added with this fix.

javascript-error avatar Mar 04 '20 18:03 javascript-error

Fixed by #769

javascript-error avatar Apr 01 '20 19:04 javascript-error