【发布时间】:2012-02-12 13:53:00
【问题描述】:
在windows上用python使用Selenium Webdriver截图时,截图直接保存到程序的路径下,有没有办法将.png文件保存到特定目录?
【问题讨论】:
标签: python selenium selenium-webdriver webdriver
在windows上用python使用Selenium Webdriver截图时,截图直接保存到程序的路径下,有没有办法将.png文件保存到特定目录?
【问题讨论】:
标签: python selenium selenium-webdriver webdriver
查看下面的 python 脚本,使用 Chrome web 驱动程序的 selenium 包拍摄 FB 主页。
脚本:
import selenium
from selenium import webdriver
import time
from time import sleep
chrome_browser = webdriver.Chrome()
chrome_browser.get('https://www.facebook.com/') # Enter to FB login page
sleep(5)
chrome_browser.save_screenshot('C:/Users/user/Desktop/demo.png') # To take FB homepage snap
chrome_browser.close() # To Close the driver connection
chrome_browser.quit() # To Close the browser
【讨论】:
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"));
【讨论】:
TakeScreenShot screenshot=new TakeScreenShot();
screenshot.screenShot("screenshots//TestScreenshot//password.png");
它会起作用的,请尝试。
【讨论】:
这将截取屏幕截图并将其放置在所选名称的目录中。
import os
driver.save_screenshot(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'NameOfScreenShotDirectory', 'PutFileNameHere'))
【讨论】:
在这里他们问了一个类似的问题,答案似乎更完整,我留下了来源:
How to take partial screenshot with Selenium WebDriver in python?
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
【讨论】:
您可以将以下函数用于相对路径,因为绝对路径不是添加到脚本中的好主意
导入
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
【讨论】:
driver.save_screenshot("path to save \\screen.jpeg")
【讨论】:
当然它现在不是实际的,但我也遇到了这个问题,而且我的方式是: 看起来“save_screenshot”在创建名称中有空格的文件时遇到了一些麻烦,因为我在文件名中添加了随机化以进行转义覆盖。
在这里,我找到了清除文件名中空格的方法 (How do I replace whitespaces with underscore and vice versa?):
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'
【讨论】:
使用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')
【讨论】:
driver.save_screenshot('/path/to/file') 在 Windows 上工作,但 driver.get_screenshot_as_file('/path/to/file') 不行。 (是的,我改成了“\\”)。但它有帮助,谢谢。你知道如何用硒检查谷歌的 ReCaptcha 吗?您将无法选择任何元素,即使 HTML 生成 <div class="google-recaptcha"> 或其他... JS 脚本也不起作用。 为了澄清,我不是指在reCaptcha中解决图像,而只是选中一个复选框“我不是机器人”。
save_screenshot calls get_screenshot_as_file,所以如果一个有效,那么另一个也应该。
driver.set_window_size(1366, 728)设置窗口大小
受此线程启发(Java 的问题相同):Take a screenshot with Selenium WebDriver
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.google.com/')
browser.save_screenshot('screenie.png')
browser.quit()
【讨论】:
是的,我们有办法使用 python webdriver 获取 .png 的屏幕截图扩展名
如果你在 python webriver 中工作,请使用下面的代码。它非常简单。
driver.save_screenshot('D\folder\filename.png')
【讨论】:
我知道您正在寻找 python 中的答案,但这里是如何在 ruby 中做到这一点..
http://watirwebdriver.com/screenshots/
如果这只能通过仅保存在当前目录中来工作。我会首先将图像分配给一个变量,然后将该变量作为 PNG 文件保存到磁盘。
例如:
image = b.screenshot.png
File.open("testfile.png", "w") do |file|
file.puts "#{image}"
end
其中 b 是 webdriver 使用的浏览器变量。我可以灵活地在“File.open”中提供绝对或相对路径,这样我就可以将图像保存在任何地方。
【讨论】: