0

I'm trying to screenshot a specific web_element using screenshot_as_png method but an error is being raised. Everything works fine when I run this program on my Windows 10, but it fails on my AWS EC2 Ubuntu 18.04

main.py

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('--disable-notifications')
options.add_argument("headless")
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
options.add_argument("--disable-gpu")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--no-sandbox")

chrome = webdriver.Chrome(options=options)
chrome.get('http://stackoverflow.com/')

element = chrome.find_element_by_css_selector('.-main.grid--cell')
element.screenshot() #Error!
chrome.quit()

The error is the following:

selenium.common.exceptions.WebDriverException: Message: unknown command: session/5c9bda106805d0d80a3f5c7b63dbf410/element/0.35398631812343306-1/screenshot

The setup I'm using:

(Session info: headless chrome=80.0.3987.87)
(Driver info: chromedriver=2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706),platform=Linux 4.15.0-1060-aws x86_64)
Thiago Benine
  • 93
  • 3
  • 8

2 Answers2

0

It seems that screenshot_as_png is not a valid command,

Try following this way:

https://pythonspot.com/selenium-take-screenshot/

Regards!

CCebrian
  • 75
  • 8
0

There is no method to the element object. It is holding the reference of the element returned by the method driver.find_element_by_css_selector('.-main.grid--cell'). Hence the error.

There is method in the WebDriver class called save_screenshot("filename.extension")

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.binary_location = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"
driver = webdriver.Chrome(options=options, executable_path="C:/Users/Downloads/chromedriver.exe", )

driver.get('http://stackoverflow.com/')

element = driver.find_element_by_css_selector('.-main.grid--cell')
#element.screenshot_as_png() # there is no method on the element object hence the error
driver.save_screenshot("screenshot.png")   # this is the method to get the screenshot

driver.quit()
Dev
  • 2,739
  • 2
  • 21
  • 34