Search code examples
pythonimageseleniumxpathscreenshot

Selenium : Unable to screenshot an element using python


So I went through the documentation and was trying to screenshot an element using

wd = webdriver.Chrome(chrome_path, chrome_options=options)
url = "https://results.vtu.ac.in/AS_CBCS/index.php"
wd.get(url)
img = wd.find_element_by_xpath('//img[@alt="CAPTCHA code"]')
img.screenshot('foo.png')

But I was getting error :

WebDriverException: Message: unknown command: session/64908e933eb1a91d71fe794e239f1687/element/0.8903743608717787-2/screenshot

WebDriverException                        Traceback (most recent call last)
<ipython-input-41-4474be757d45> in <module>()
     10 
     11 
---> 12 img.screenshot('foo.png')

c:\users\himanshu poddar\appdata\local\programs\python\python36-32\lib\site-packages\selenium\webdriver\remote\webelement.py in screenshot(self, filename)
    451             warnings.warn("name used for saved screenshot does not match file "
    452                           "type. It should end with a `.png` extension", UserWarning)
--> 453         png = self.screenshot_as_png
    454         try:
    455             with open(filename, 'wb') as f:

c:\users\himanshu poddar\appdata\local\programs\python\python36-32\lib\site-packages\selenium\webdriver\remote\webelement.py in screenshot_as_png(self)
    433             element_png = element.screenshot_as_png
    434         """
--> 435         return base64.b64decode(self.screenshot_as_base64.encode('ascii'))
    436 
    437     def screenshot(self, filename):

c:\users\himanshu poddar\appdata\local\programs\python\python36-32\lib\site-packages\selenium\webdriver\remote\webelement.py in screenshot_as_base64(self)
    423             img_b64 = element.screenshot_as_base64
    424         """
--> 425         return self._execute(Command.ELEMENT_SCREENSHOT)['value']
    426 
    427     @property

c:\users\himanshu poddar\appdata\local\programs\python\python36-32\lib\site-packages\selenium\webdriver\remote\webelement.py in _execute(self, command, params)
    499             params = {}
    500         params['id'] = self._id
--> 501         return self._parent.execute(command, params)
    502 
    503     def find_element(self, by=By.ID, value=None):

c:\users\himanshu poddar\appdata\local\programs\python\python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params)
    309         response = self.command_executor.execute(driver_command, params)
    310         if response:
--> 311             self.error_handler.check_response(response)
    312             response['value'] = self._unwrap_value(
    313                 response.get('value', None))

c:\users\himanshu poddar\appdata\local\programs\python\python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response)
    206             if exception_class == ErrorInResponseException:
    207                 raise exception_class(response, value)
--> 208             raise exception_class(value)
    209         if message == "" and 'message' in value:
    210             message = value['message']

WebDriverException: Message: unknown command: session/7446082ee2eab255d16e7ac489d4b9b1/element/0.7847029784212665-2/screenshot


s.screenshot('screenshot.png')
---------------------------------------------------------------------------
WebDriverException                        Traceback (most recent call last)
<ipython-input-29-53a118d5bd49> in <module>()
----> 1 s.screenshot('screenshot.png')

c:\users\himanshu poddar\appdata\local\programs\python\python36-32\lib\site-packages\selenium\webdriver\remote\webelement.py in screenshot(self, filename)
    451             warnings.warn("name used for saved screenshot does not match file "
    452                           "type. It should end with a `.png` extension", UserWarning)
--> 453         png = self.screenshot_as_png
    454         try:
    455             with open(filename, 'wb') as f:

c:\users\himanshu poddar\appdata\local\programs\python\python36-32\lib\site-packages\selenium\webdriver\remote\webelement.py in screenshot_as_png(self)
    433             element_png = element.screenshot_as_png
    434         """
--> 435         return base64.b64decode(self.screenshot_as_base64.encode('ascii'))
    436 
    437     def screenshot(self, filename):

c:\users\himanshu poddar\appdata\local\programs\python\python36-32\lib\site-packages\selenium\webdriver\remote\webelement.py in screenshot_as_base64(self)
    423             img_b64 = element.screenshot_as_base64
    424         """
--> 425         return self._execute(Command.ELEMENT_SCREENSHOT)['value']
    426 
    427     @property

c:\users\himanshu poddar\appdata\local\programs\python\python36-32\lib\site-packages\selenium\webdriver\remote\webelement.py in _execute(self, command, params)
    499             params = {}
    500         params['id'] = self._id
--> 501         return self._parent.execute(command, params)
    502 
    503     def find_element(self, by=By.ID, value=None):

c:\users\himanshu poddar\appdata\local\programs\python\python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params)
    309         response = self.command_executor.execute(driver_command, params)
    310         if response:
--> 311             self.error_handler.check_response(response)
    312             response['value'] = self._unwrap_value(
    313                 response.get('value', None))

c:\users\himanshu poddar\appdata\local\programs\python\python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response)
    206             if exception_class == ErrorInResponseException:
    207                 raise exception_class(response, value)
--> 208             raise exception_class(value)
    209         if message == "" and 'message' in value:
    210             message = value['message']

WebDriverException: Message: unknown command: session/64908e933eb1a91d71fe794e239f1687/element/0.8903743608717787-2/screenshot

Solution

  • Chrome version : 85.0.4183.121 (Official Build) (64-bit)
    Chrome driver version :  2.38.552522
    

    So installing and updating my chrome driver version to (85.X.X.X) solved my issue.