- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
据我了解,ChromeDriver 本身不会设置背景,CSS 会。因此,如果背景是透明的,为什么我没有得到透明的屏幕截图?
这是所谓的透明网站的屏幕截图:
相同的屏幕截图,但背景中有一个红色 div 以显示透明度应位于的位置:
这是我的代码:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from datetime import datetime
options = webdriver.ChromeOptions()
options.add_argument('headless')
driver = webdriver.Chrome(chrome_options=options)
driver.set_window_size(2560, 1600)
driver.get('https://twitter.com/realDonaldTrump/status/516382177798680576')
# driver.execute_script("$('body').append(`<div style='background: red; width: 100%; height: 100%;'></div>`);")
driver.save_screenshot('screenshots/' + str(datetime.now()) + '.png')
driver.quit()
我如何才能创建该屏幕截图的透明版本?
最佳答案
一种方法是通过将 alpha 字节设置为 0,将屏幕截图中的每个白色像素转换为透明像素:
from selenium import webdriver
from PIL import Image
from io import BytesIO # python 3
import numpy as np
def remove_color(img, rgba):
data = np.array(img.convert('RGBA')) # rgba array from image
pixels = data.view(dtype=np.uint32)[...,0] # pixels as rgba uint32
data[...,3] = np.where(pixels == np.uint32(rgba), np.uint8(0), np.uint8(255)) # set alpha channel
return Image.fromarray(data)
driver = webdriver.Chrome()
driver.get("http://www.bbc.co.uk/news")
# take screenshot with a transparent background
with Image.open(BytesIO(driver.get_screenshot_as_png())) as img :
with remove_color(img, 0xffffffff) as img2:
img2.save(r"C:\temp\screenshot.png")
from selenium import webdriver
import json
def send(cmd, params={}):
resource = "/session/%s/chromium/send_command_and_get_result" % driver.session_id
url = driver.command_executor._url + resource
body = json.dumps({'cmd':cmd, 'params': params})
response = driver.command_executor._request('POST', url, body)
if response['status']: raise Exception(response.get('value'))
return response.get('value')
options = webdriver.ChromeOptions()
options.add_argument("disable-gpu")
options.add_argument("disable-infobars")
driver = webdriver.Chrome(chrome_options=options)
driver.get("http://www.bbc.co.uk/news")
# take screenshot with a transparent background
send("Emulation.setDefaultBackgroundColorOverride", {'color': {'r': 0, 'g': 0, 'b': 0, 'a': 0}})
driver.get_screenshot_as_file(r"C:\temp\screenshot.png")
send("Emulation.setDefaultBackgroundColorOverride") # restore
关于python-3.x - 通过 Selenium 使用 headless ChromeDriver 的透明屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46656622/
我正在尝试使用 firefox headless 在 headless (headless) redhat linux 构建机器上运行 selenium 测试。我创建驱动程序的方法如下所示: priv
关于这个主题有很多东西可以找到,但无法弄清楚。我需要滚动到(不太长)无限滚动页面的末尾。我有 2 个选项可以使用 chrome 非 headless (headless)但似乎不能 headless
我在远程服务器上运行 OpenFOAM,基本上设法通过 paraview 的 pvserver 可视化结果 as described here .然而,在连接后,客户端产生 Server DISPLA
我想在 headless 模式下截取 Android 设备的屏幕截图,也就是说我是这样创建的: echo no | /opt/android/android-sdk-linux/tools/andro
主要区别在于,基于GUI和非GUI(Headless)执行。 我正在寻找所有Headless浏览器之间的差异,但是很遗憾,我没有找到任何差异。我一个接一个地经历,这使我更加困惑。如果有人可以分享具有差
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭10 年前。 Improve th
我需要在没有 X 服务器的情况下执行 java 图像裁剪和调整大小。 我尝试了几种方法。下面的第一种方法有效,但输出了一个相当难看的调整大小的图像(可能使用最近邻算法来调整大小: static Buf
过去几天我一直在使用 Selenium、Tor 和 Firefox 作为多个任务的组合。我已经设法用 Python 编写了一个简单的脚本,它通过 Selenium 控制 Firefox,而 Firef
我正在使用 pygame 的操纵杆 api 在 headless (headless)系统上对我的项目使用操纵杆,但是 pygame 需要一个“屏幕”,所以我设置了一个虚拟视频系统来克服这个问题。它工
我想使用 headless firefox 在 macos 上捕获网页的图像。 这是我执行的命令:/Applications/Firefox.app/Contents/MacOS/firefox-bi
我正在使用带有 headless-chromium-php 的 google chrome headless (headless)浏览器 导航到某些网站,但它总是被验证码检测到 我尝试使用此 plug
是否有可能使用 Octave headless。 像这样的东西 octave result.txt 最佳答案 使用 octave --silent --eval 5+4 > result.txt 你会
我目前正在尝试在 headless (headless)模式下运行应用程序,我定义了后台回调: void callbackInBackground() { // Invoked from the s
我正在使用LibGDX headless backend运行jUnit测试。这在某些测试中效果很好,但是如果我尝试创建new Texture('myTexture.png');,则会收到NullPoi
我想在这个页面上使用 Selenium:https://www.avis.com/en/home 如果没有 headless (headless)模式,该代码一切正常: import requests
在Jasmine headless (headless)Webkit中运行测试时,我遇到了一个简单的TypeError: 'undefined' is not an object失败。但是没有提示在哪
我负责测试一个大量使用 AJAX 的企业 Web 应用程序。我需要构建一个系统,允许在没有人工干预的情况下连续运行测试。目前我最感兴趣的是负载测试,但我希望用于生成负载的相同脚本用于功能测试。 目前用
TL; DR:我可以配置一个容器来原生访问VGA,以覆盖主机视频输出吗? 我正在考虑处置低功耗的XenServer(以前为ESXi)白盒以设置docker最小安装(例如CoreOS,RancherOs
我正在尝试 headless (headless)运行我的测试,并将我的两个测试套件分片以并行运行它们。在我的本地计算机上,它们并行运行,但在这种 headless (headless)设置中,它们一
仍在尝试为大型大学项目(RCP 产品)建立 headless (headless)构建。 每个 Eclipse 用户都知道以下手动功能:“文件 --> 导入 --> 将现有项目导入工作区”以及“构建工
我是一名优秀的程序员,十分优秀!