问与答 python-Webdriver屏幕截图

parker · 2020-03-06 17:33:22 · 热度: 35

在带有 python 的Windows上使用Selenium Webdriver截屏时,截屏直接保存到程序路径中,是否可以将.png文件保存到特定目录?

猜你喜欢:
共收到 11 条回复
cedric #1 · 2020-03-06 17:33:22

使用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')
kim #2 · 2020-03-06 17:33:24

受此线程启发(对于Java同样的问题):使用Selenium WebDriver截屏

from selenium import webdriver

browser = webdriver.Firefox()
browser.get('http://www.google.com/')
browser.save_screenshot('screenie.png')
browser.quit()
venjo #3 · 2020-03-06 17:33:25

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

如果您在python webriver中工作,请使用以下代码。它非常简单。

driver.save_screenshot('D\folder\filename.png')
edwin #4 · 2020-03-06 17:33:26

当然现在还不实际,但是我也遇到了这个问题,我的方法是:看起来“ 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

然后

driver.save_screenshot('c:\\pytest_screenshots\\%s' % screen_name)

哪里

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

screen_name = self.urlify(datetime_now('screen')) + '.png'
earnest #5 · 2020-03-06 17:33:28
driver.save_screenshot("path to save \\screen.jpeg")
terence #6 · 2020-03-06 17:33:29

在这里,他们提出了类似的问题,答案似乎更加完整,我离开了消息来源:

如何在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
rolando #7 · 2020-03-06 17:33:30

您可以将以下函数用于相对路径,因为绝对路径不是在脚本中添加的好主意

进口

import sys, os

使用如下代码:

ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
screenshotpath = os.path.join(os.path.sep, ROOT_DIR,'Screenshots'+ os.sep)
driver.get_screenshot_as_file(screenshotpath+"testPngFunction.png")

确保您创建存在.py文件的文件夹。

os.path.join还阻止您在跨平台(如UNIX和Windows)中运行脚本。 它将在运行时根据操作系统生成路径分隔符。 os.sep与Java中的File.separtor类似

kerwin #8 · 2020-03-06 17:33:32

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

import os
driver.save_screenshot(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'NameOfScreenShotDirectory', 'PutFileNameHere'))
wyatt #9 · 2020-03-06 17:33:33
TakeScreenShot screenshot=new TakeScreenShot();
screenshot.screenShot("screenshots//TestScreenshot//password.png");

它将起作用,请尝试。

colby #10 · 2020-03-06 17:33:34
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\NewFolder\\screenshot1.jpg"));
tavis #11 · 2020-03-06 17:33:36

我了解您正在使用python寻找答案,但是这是如何在ruby中做到的。

[HTTP://蛙体如Web driver.com/screenshots/]

如果那只能通过仅保存在当前目录中而起作用。.我首先将图像分配给变量,然后将该变量作为PNG文件保存到磁盘。

例如:

 image = b.screenshot.png

 File.open("testfile.png", "w") do |file|
  file.puts "#{image}"
 end

其中b是webdriver使用的浏览器变量。 我可以在“ File.open”中提供绝对路径或相对路径,因此可以将图像保存在任何地方。

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册