Webdriver截图

use*_*578 49 python selenium webdriver selenium-webdriver

在使用python的Windows上使用Selenium Webdriver截取屏幕截图时,屏幕截图直接保存到程序的路径中,有没有办法将.png文件保存到特定目录?

unu*_*tbu 66

使用driver.save_screenshot('/path/to/file')driver.get_screenshot_as_file('/path/to/file'):

import selenium.webdriver as webdriver
import contextlib

@contextlib.contextmanager
def quitting(thing):
    yield thing
    thing.quit()

with quitting(webdriver.Firefox()) as driver:
    driver.implicitly_wait(10)
    driver.get('http://www.google.com')
    driver.get_screenshot_as_file('/tmp/google.png') 
    # driver.save_screenshot('/tmp/google.png')
Run Code Online (Sandbox Code Playgroud)


Ran*_*ler 28

灵感来自这个线程(Java的相同问题):使用Selenium WebDriver截取屏幕截图

from selenium import webdriver

browser = webdriver.Firefox()
browser.get('http://www.google.com/')
browser.save_screenshot('screenie.png')
browser.quit()
Run Code Online (Sandbox Code Playgroud)


Kv.*_*mar 9

是的,我们有办法使用python webdriver获取.png的屏幕截图扩展

如果你在python webriver.it中工作,请使用下面的代码非常简单.

driver.save_screenshot('D\folder\filename.png')
Run Code Online (Sandbox Code Playgroud)


小智 7

driver.save_screenshot("path to save \\screen.jpeg")
Run Code Online (Sandbox Code Playgroud)


Vla*_*nov 5

当然,它现在不是实际的,但我也遇到了这个问题和我的方式:看起来“save_screenshot”在创建名称中带有空格的文件时遇到了一些麻烦,同时我向文件名添加了随机化以转义覆盖。

在这里,我找到了清理文件名中空格的方法(如何用下划线替换空格,反之亦然?):

def urlify(self, s):
    # Remove all non-word characters (everything except numbers and letters)
    s = re.sub(r"[^\w\s]", '', s)
    # Replace all runs of whitespace with a single dash
    s = re.sub(r"\s+", '-', s)
    return s
Run Code Online (Sandbox Code Playgroud)

然后

driver.save_screenshot('c:\\pytest_screenshots\\%s' % screen_name)
Run Code Online (Sandbox Code Playgroud)

在哪里

def datetime_now(prefix):
    symbols = str(datetime.datetime.now())
    return prefix + "-" + "".join(symbols)

screen_name = self.urlify(datetime_now('screen')) + '.png'
Run Code Online (Sandbox Code Playgroud)


小智 5

这里他们问了一个类似的问题,答案似乎更完整,我留下了出处:

如何在 python 中使用 Selenium WebDriver 截取部分屏幕截图?

from selenium import webdriver
from PIL import Image
from io import BytesIO

fox = webdriver.Firefox()
fox.get('http://stackoverflow.com/')

# now that we have the preliminary stuff out of the way time to get that image :D
element = fox.find_element_by_id('hlogo') # find part of the page you want image of
location = element.location
size = element.size
png = fox.get_screenshot_as_png() # saves screenshot of entire page
fox.quit()

im = Image.open(BytesIO(png)) # uses PIL library to open image in memory

left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']


im = im.crop((left, top, right, bottom)) # defines crop points
im.save('screenshot.png') # saves new cropped image
Run Code Online (Sandbox Code Playgroud)


Ger*_* Mc 5

这将截取屏幕截图并将其放置在所选名称的目录中。

import os
driver.save_screenshot(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'NameOfScreenShotDirectory', 'PutFileNameHere'))
Run Code Online (Sandbox Code Playgroud)

  • “这将截取屏幕截图并将其放置在选定名称的目录中。” 我想对大多数人来说都很清楚 (2认同)