在使用python的Windows上使用Selenium Webdriver截取屏幕截图时,屏幕截图直接保存到程序的路径中,有没有办法将.png文件保存到特定目录?
是否可以在框架集中仅使用一个框架(而不是完整窗口)的WebDriver截屏?
或者,是否可以为屏幕截图定义窗口的坐标或之后裁剪图像?
我试图将canvas元素保存为png图像.这是我的代码,但不幸的是,它不起作用:
import time
from selenium import webdriver
# From PIL import Imag.
driver = webdriver.Firefox()
driver.get('http://www.agar.io')
driver.maximize_window()
driver.find_element_by_id('freeCoins').click()
time.sleep(2)
# The part below does not seem to work properly.
driver.execute_script('function download_image(){var canvas = document.getElementByTagName("canvas");canvas.toBlob(function(blob) {saveAs(blob, "../images/output.png");}, "image/png");};')
Run Code Online (Sandbox Code Playgroud)
我想在Python中看到解决方案.我还希望在截图结束时看到一个不需要裁剪的解决方案.
我想截图Selenium中的一个元素,根据文档,每个WebElement都有一个功能:
屏幕截图(文件名)
将当前元素的屏幕截图保存为 PNG 图像文件。如果有任何 IOError,则返回 False,否则返回 True。在文件名中使用完整路径。
参数:文件名:您希望将屏幕截图保存到的完整路径。这应该以 .png 扩展名结尾
用法: element.screenshot('/Screenshots/foo.png')
但是,当我在我的程序中使用这个函数时:
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')
Run Code Online (Sandbox Code Playgroud)
它引发这样的错误:
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 …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用python与硒结合从网页中截取所需部分的屏幕截图。当我执行脚本时,确实会得到一个屏幕截图,但这并不是我打算让脚本抓取的。
我希望抓住Desired one下面显示的部分而不是Current output。要获得screenshot确切的显示方式Desired one,脚本必须单击该图像旁边的+按钮以使其更宽,以使图库中的点可见。
但是,当前方法仅获取该图像的部分部分的屏幕截图。而且,它也抓住了不需要的部分。
我尝试过:
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
link = 'https://www1.ticketmaster.com/celine-dion-courage-world-tour/event/0600567BFDB0AB48'
def start_script():
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
driver = webdriver.Chrome(options=options)
return driver
def get_links(url):
driver = start_script()
driver.get(url)
try:
button = WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.CSS_SELECTOR,"#landingPricingMessage button.pricing-landing__modal-btn")))
except Exception: button = ""
if button: button.click()
try:
zoom = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR,"button[class='zoomer__control--zoomin'] > svg[class='zoomin']")))
except Exception: zoom …Run Code Online (Sandbox Code Playgroud) from selenium import webdriver
browser = webdriver.Chrome()
browser.get("http://www.baidu.com")
browser.find_element_by_id('su').screenshot('E:/test.png')
browser.quit()
Run Code Online (Sandbox Code Playgroud)
当我运行上面的代码时,我得到了错误,我的python版本是2.7.13,selenium是3.1
[0315/220804.111:错误:angle_platform_impl.cc(33)] ANGLE显示:: initialize err或5:DXGI 1.2需要呈现给另一个进程拥有的HWND.[0315/220804.111:错误:gl_surface_egl.cc(646)] eglInitialize D3D11失败并出现EGL_NOT_INITIALIZED,尝试下一个显示类型Traceback(最近一次调用最后一次):文件"C:\ Users\Administrator\Desktop\test.py",第5行,在browser.find_element_by_id('su').截图('E:/test.png')文件"C:\ Python27\lib\site-packages\selenium\webdriver\remote\webelement.py",第443行,截图png = self.screenshot_as_png文件"C:\ Python27\lib\site-packages\selenium\webdriver\remote\webelement.py",第430行,在screenshot_as_png中返回base64.b64decode(self.screenshot_as_base64.encode('ascii) '))文件"C:\ Python27\lib\site-packages\selenium\webdriver\remote\webelement.py",第420行,在screenshot_as_base64中返回self._execute(Command.ELEMENT_SCREENSHOT)['value']文件"C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py",第491行,在_execute中返回self._parent.execute(command,params)文件"C:\ Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py",l ine 238,在exe中 可爱的self.error_handler.check_response(响应)文件"C:\ Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py",第164行,在check_response中引发exception_class(value)selenium.common.exceptions.WebDriverException :消息:未知命令:session /4a58c13f918aa319b2df6ef70ac2ca51/element/0.4724184220629968-1/screenshot
我正在尝试使用Selenium下载验证码图像,但是,我得到的图像与浏览器中显示的图像不同.如果我尝试再次下载图像,而不更改浏览器,我会得到一个不同的图像.
有什么想法吗?
from selenium import webdriver
import urllib
driver = webdriver.Firefox()
driver.get("http://sistemas.cvm.gov.br/?fundosreg")
# Change frame.
driver.switch_to.frame("Main")
# Download image/captcha.
img = driver.find_element_by_xpath(".//*[@id='trRandom3']/td[2]/img")
src = img.get_attribute('src')
urllib.request.urlretrieve(src, "captcha.jpeg")
Run Code Online (Sandbox Code Playgroud) 我使用Bokeh包生成地图以显示模拟结果.输出是具有交互性的html格式的单独地图.各个地图需要交互性.
请参阅此链接以获取示例:
http://bokeh.pydata.org/en/0.10.0/docs/gallery/texas.html
模拟可以自动设置为运行多次,并为每次运行生成一个映射.这可能是100的地图.我希望能够将地图拼接在一起以创建电影 - 这不需要交互性.Bokeh具有通过浏览器创建PNG文件的功能,因此可以手动将每个地图保存为文件,并使用ffmpeg创建电影.但是,如果你需要为100个文件做这个,那么这不是一个真正的选择.目前没有办法通过Bokeh自动生成PNG文件,但我相信它会在某些时候添加.
所以我需要一个解决方法.我的想法是从他们存储在本地驱动器上的位置打开每个html文件,拍摄屏幕截图,裁剪图像以保留所需的部分并保存.但我还没有找到一个有效的解决方案.
裁剪图像很简单:
from PIL import Image
img = Image.open(file_name)
box = (1, 1, 1000, 1000)
area = img.crop(box)
area.save('saved_image', 'jpeg')
Run Code Online (Sandbox Code Playgroud)
我的问题是打开html文件并首先拍摄屏幕截图以提供上述代码.
为此我尝试了以下内容,但都需要URL而不是html文件.另外两个都使用Firefox不适合我,但我已经安装了chrome并适当地修改了代码.
如何在python中使用Selenium WebDriver进行部分截图?
http://www.idiotinside.com/2015/10/20/take-screenshot-selenium-python/
我的代码是:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('file_name')
driver.save_screenshot('image.png')
driver.quit()
Run Code Online (Sandbox Code Playgroud)
哪个回报:
{"code":-32603,"message":"Cannot navigate to invalid URL"}
Run Code Online (Sandbox Code Playgroud)
显然,文件名不是网址,因此很清楚.如果你把它传递给一个网站,它的工作正常.获取html加载和拍照的任何帮助将不胜感激!它不必涉及Selenium.
我正在尝试使用此问题答案中的代码来截取网页上特定元素的屏幕截图。当我几天前测试它时,这最初是有效的,但现在它产生的区域始终位于目标元素的上方和左侧。
原始代码的输出对调试没有多大帮助,因此我将其更改为在该区域周围绘制一个矩形,而不是裁剪它。
例子:
from selenium import webdriver
from PIL import Image, ImageDraw
from io import BytesIO
browser = webdriver.Chrome()
browser.get('http://www.google.com')
logo = browser.find_element_by_id('hplogo') #id of 'Google' image
location = logo.location
size = logo.size
im = Image.open(BytesIO(browser.get_screenshot_as_png()))
draw = ImageDraw.Draw(im)
draw.rectangle(((location['x'], location['y']), (location['x'] + size['width'], location['y'] + size['height'])), outline='black')
im.show()
browser.quit()
Run Code Online (Sandbox Code Playgroud)
绘制的框似乎具有正确的长宽比,但位置和大小不正确。如果您能解释导致此问题的原因以及解决此问题的任何帮助,我将不胜感激。
我正在尝试使用 OpenCV 和 selenium 将 html 中的特定元素保存为图像文件。但无法保存文件。
from selenium import webdriver
import cv2
import numpy as np
browser = webdriver.Firefox()
browser.get(<URl with image>)
# Element to be saved
element = browser.find_element_by_id(<id of element>)
png = browser.get_screenshot_as_png()
location = element.location
size = element.size
nparr = np.frombuffer(png, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']
im = img[left:right, top:bottom]
cv2.imwrite('filename.png',im)
Run Code Online (Sandbox Code Playgroud)
'filename.png'目前运行此脚本没有图像数据。
我可以使用Firefox.get_screenshot_as_file('2.png')截取整个页面,但是当我使用passage.screenshot('1.png')截取网页元素时,它总是引发此异常:
selenium.common.exceptions.WebDriverException: Message: Unrecognized command: GET /session/284283fa-53fc-4b33-b329-e6e888dbdcb0/screenshot/{35834cf1-c9c7-4129-99b1-24f30c6b56e6}
Run Code Online (Sandbox Code Playgroud) selenium ×11
python ×10
webdriver ×2
bokeh ×1
canvas ×1
crop ×1
firefox ×1
html ×1
javascript ×1
numpy ×1
opencv ×1
png ×1
python-3.x ×1
screenshot ×1
web-scraping ×1