Skip to content

[🐛 Bug]: Wrong type for screenshot_as_png #10623

@eNcacz

Description

@eNcacz

What happened?

The WebElement class in selenium/webdriver/remote/webelement.py file has a property screenshot_as_png which declare the return type as str, but it returns the output of b64decode method which returns bytes.

See the screenshot_as_png source code in selenium 4.1.5 package. It looks like this one:

    @property
    def screenshot_as_png(self) -> str:
        """
        Gets the screenshot of the current element as a binary data.

        :Usage:
            ::

                element_png = element.screenshot_as_png
        """
        return b64decode(self.screenshot_as_base64.encode('ascii'))

So the screenshot_as_png seems to return str but in runtime it returns bytes. The str type for return value should be changed to bytes.

If you run the example below, then it works as expected - the screenshot variable is bytes but PyCharm (and mypy) complains for line 10 that Expected type 'bytes', got 'str' instead which is not true in runtime.

How can we reproduce the issue?

from chromedriver_binary.utils import get_chromedriver_path
from selenium import webdriver
from selenium.webdriver.common.by import By


def test():
  driver = webdriver.Chrome(executable_path=get_chromedriver_path() + '/chromedriver')
  driver.get('http://www.example.com/')
  element = driver.find_element(by=By.TAG_NAME, value='h1')
  screenshot:bytes = element.screenshot_as_png
  print(type(screenshot))


if __name__ == '__main__':
  test()

Relevant log output

No log needed.

Operating System

Gentoo Linux

Selenium version

Python 4.1.5

What are the browser(s) and version(s) where you see this issue?

it's not relevant

What are the browser driver(s) and version(s) where you see this issue?

it's not relevant

Are you using Selenium Grid?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-needs-triagingA Selenium member will evaluate this soon!C-pyPython BindingsI-defectSomething is not working as intended

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions