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
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
What happened?
The
WebElementclass inselenium/webdriver/remote/webelement.pyfile has a propertyscreenshot_as_pngwhich declare the return type asstr, but it returns the output ofb64decodemethod which returnsbytes.See the
screenshot_as_pngsource code in selenium 4.1.5 package. It looks like this one:So the
screenshot_as_pngseems to returnstrbut in runtime it returnsbytes. Thestrtype for return value should be changed tobytes.If you run the example below, then it works as expected - the screenshot variable is
bytesbut PyCharm (and mypy) complains for line 10 thatExpected type 'bytes', got 'str' insteadwhich is not true in runtime.How can we reproduce the issue?
Relevant log output
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