python webdriver save_python-Webdriver屏幕截图

python-Webdriver屏幕截图

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

11个解决方案

65 votes

使用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')

unutbu answered 2020-02-20T10:06:35Z

28 votes

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

from selenium import webdriver

browser = webdriver.Firefox()

browser.get('http://www.google.com/')

browser.save_screenshot('screenie.png')

browser.quit()

Ran Adler answered 2020-02-20T10:06:55Z

9 votes

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

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

driver.save_screenshot('D\folder\filename.png')

Kv.senthilkumar answered 2020-02-20T10:07:19Z

3 votes

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

Vladimir Kolenov answered 2020-02-20T10:07:52Z

3 votes

driver.save_screenshot("path to save \\screen.jpeg")

Shaik answered 2020-02-20T10:08:08Z

3 votes

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

如何在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

gabriel de la cruz answered 2020-02-20T10:08:32Z

2 votes

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

进口

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类似

Shubham Jain answered 2020-02-20T10:09:10Z

2 votes

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

import os

driver.save_screenshot(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'NameOfScreenShotDirectory', 'PutFileNameHere'))

Ger Mc answered 2020-02-20T10:09:30Z

1 votes

TakeScreenShot screenshot=new TakeScreenShot();

screenshot.screenShot("screenshots//TestScreenshot//password.png");

它将起作用,请尝试。

suriya kanagaraj answered 2020-02-20T10:09:49Z

0 votes

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"));

Dharit Mehta answered 2020-02-20T10:10:05Z

-7 votes

我了解您正在使用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”中提供绝对路径或相对路径,因此可以将图像保存在任何地方。

sambehera answered 2020-02-20T10:10:43Z

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值