0

It gives this error for CSS that exist but do not appear in the site's interface.

My code (site and css are examples):

driver.get("www.example.com")
element = driver.find_element_by_css_selector("css").screenshot_as_png

Error:

selenium.common.exceptions.WebDriverException: Message: unknown error: unhandled inspector error: {"code":-32000,"message":"Cannot take screenshot with 0 width."}

but if the link changes, ie tries to take a screenshot from a link where the bodies appear, it works correctly.

what i tried to do is continue without any error. because when it fails, the script stops.

Solution

After some research I found the solution. CODE (site and css are examples):

driver.get("www.example.com")
try:
    WebDriverWait(driver, 0).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "css")))
    element = driver.find_element_by_css_selector("css").screenshot_as_png
    print ("element visible")

except TimeoutException:
    print ("element not visible")

2 Answers2

1

There is a property called

WebElement.screenshot_as_png

This returns png as binary data , if you want to store the image as file you can use .screenshot() method

WebElement.screenshot("hi.png")

you can see all avaialble methods and properties of webelement at :

https://www.selenium.dev/selenium/docs/api/py/webdriver_remote/selenium.webdriver.remote.webelement.html

example:

driver.find_element_by_id("anyid").screenshot_as_png

driver.find_element_by_id("anyid").screenshot("hi.png")

As per the error the screenshot not working because of its width , see if the element is properly visible before you take screenshot

use webdriver wait:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(browser, 10)

elem= wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'somelocator')))
PDHide
  • 18,113
  • 2
  • 31
  • 46
  • yes i used something close to this and fixed it. Thank you so much. but what I want to do is take a screenshot of where the element is located. I save this later. So there is no problem right now. – Hengen Asdwdrasd Feb 19 '21 at 22:09
  • @HengenAsdwdrasd this does taht you can use any locator – PDHide Feb 19 '21 at 22:10
  • you can accept the answer by pressing the tikc sign if it helped – PDHide Feb 19 '21 at 22:11
0

There is no attribute as screenshot_as_png for the remote webdriver but the method is get_screenshot_as_png() which gets the screenshot of the current window as a binary data.

  • Usage:

    driver.get_screenshot_as_png()
    

Solution

Essentially get_screenshot_as_png() is a webdriver method and your effective code block will be:

element_screenshot_png = driver.get_screenshot_as_png()

Reference

You can find a couple of relevant detailed discussion in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I don't want to take a full screenshot. Also my error seems to be related to the screenshot but it is not. thank you anyway. I solved the problem and will explain the solution soon. – Hengen Asdwdrasd Feb 19 '21 at 21:44
  • thats incorrect statement , https://www.selenium.dev/selenium/docs/api/py/webdriver_remote/selenium.webdriver.remote.webelement.html , it has that property – PDHide Feb 19 '21 at 22:00