如何使用Python在Selenium中对指定的WebElement进行屏幕截图

How to screenshot a specified WebElement in Selenium using Python

我想截图Selenium中的一个元素,根据文档,每个WebElement都有一个功能:

screenshot(filename)

Saves a screenshot of the current element to a PNG image file. Returns
False if there is any IOError, else returns True. Use full paths in your filename.

Args:
filename: The full path you wish to save your screenshot to. This should end with a .png extension

Usage:
element.screenshot(a€?/Screenshots/foo.pnga€?)

但是,当我在程序中使用此功能时:

1
2
3
4
5
6
7
8
9
10
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep


url='http://www.google.com'
browser = webdriver.Chrome()  
browser.get(url)
content = browser.find_element_by_id('searchform')
content.screenshot('/home/ding/Pictures/shot.png')

它引发如下错误:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Traceback (most recent call last):

  File"<ipython-input-8-309cb404878d>", line 11, in <module>
    content.screenshot('/home/ding/Pictures/shot.png')

  File"/home/ding/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 453, in screenshot
    png = self.screenshot_as_png

  File"/home/ding/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 435, in screenshot_as_png
    return base64.b64decode(self.screenshot_as_base64.encode('ascii'))

  File"/home/ding/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 425, in screenshot_as_base64
    return self._execute(Command.ELEMENT_SCREENSHOT)['value']

  File"/home/ding/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 501, in _execute
    return self._parent.execute(command, params)

  File"/home/ding/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 308, in execute
    self.error_handler.check_response(response)

  File"/home/ding/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 165, in check_response
    raise exception_class(value)

WebDriverException: unknown command: session/efbca24571c5332230f4d032ae04787c/element/0.7487814861441955-1/screenshot

如何解决此问题并在Python中使用Selenium拍摄元素的屏幕截图?


此代码非常适合在Firefox浏览器中拍摄特定元素的屏幕截图。

1
2
3
4
5
6
7
8
9
10
from selenium import webdriver
import io
from PIL import Image

fox = webdriver.Firefox()
fox.get('http://stackoverflow.com/')
image = fox.find_element_by_id('hlogo').screenshot_as_png
imageStream = io.BytesIO(image)
im = Image.open(imageStream)
im.save(image_path)


导入其他库来写文件对我来说似乎很愚蠢...

使用内置的open

1
2
3
button_element = self._driver.find_element_by_class_name("a-button-input")
with open("element.png","wb") as elem_file:
    elem_file.write(button_element.screenshot_as_png)

Amazon元素输出:

enter

1
content.screenshot('/home/ding/Pictures/shot.png')

使用:

1
content.save_screenshot('/home/ding/Pictures/shot.png')

1
2
3
4
5
6
7
8
9
10
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep


url="http://www.google.com"
browser = webdriver.Chrome("c:\\chrome\\chromedriver.exe")
browser.get(url)
browser.find_element_by_id('searchform')
browser.save_screenshot("C://Demo//shot.png")