简体   繁体   English

在Selenium [Python]中使用PhantomJS进行透明屏幕截图?

[英]Transparent screenshot with PhantomJS in Selenium [Python]?

When I take a screenshot in with PhantomJS as the webdriver in Selenium, all I get is a transparent background. 当我用PhantomJS作为Selenium中的webdriver进行截图时,我得到的只是透明背景。 Any clue why? 有什么线索的原因? It works with pages such as Google.com, but not kahoot.it, the one I want. 它适用于Google.com等网页,但不适用于我想要的kahoot.it。 It also works with everything else I need in Firefox, but not in PhantomJS. 它也适用于我在Firefox中需要的所有其他内容,但不适用于PhantomJS。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import time

dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = ("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36")

This is the code 这是代码

driver = webdriver.PhantomJS(desired_capabilities=dcap)
time.sleep(12)
driver.set_window_size(1024, 768)
driver.get('http://www.kahoot.it')

driver.save_screenshot('testing.png')

Any help would be much appreciated! 任何帮助将非常感激! :) :)

Your problem is about your website. 您的问题与您的网站有关。

You typed it as http://www.kahoot.it but in the end, it does redirect to https site. 您将其键入为http://www.kahoot.it但最终会重定向到https站点。 So, your PhantomJS is getting errors from ssl version or ssl itself if where errors. 因此,如果出现错误,您的PhantomJS会从ssl版本或ssl本身获得错误。

Modify your webdriver.PhantomJS() to that: driver = webdriver.PhantomJS(desired_capabilities=dcap, service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any', '--web-security=false']) and all should run ok. 将你的webdriver.PhantomJS()修改为: driver = webdriver.PhantomJS(desired_capabilities=dcap, service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any', '--web-security=false'])所有应该运行正常。

On the other hand, if you don't like transparent background set your own with: driver.execute_script('document.body.style.background = "black"') . 另一方面,如果你不喜欢透明背景设置自己的: driver.execute_script('document.body.style.background = "black"')

With the first example you'll see only left frame blacked, that's because a top item has been set to white background. 在第一个示例中,您将看到只有左框架变黑,这是因为顶部项目已设置为白色背景。 On the kahoot example you can't set it because that webpage has it's own javascript autochange script. 在kahoot示例中,您无法设置它,因为该网页具有自己的javascript自动转换脚本。 You should remove it before attemp change it, otherwise your setting will be overrided soon or later. 您应该在尝试更改之前将其移除,否则您的设置将很快或稍后被覆盖。

Full code, ready to run: 完整代码,准备运行:

#!/usr/bin/env python
#! -*- coding: utf-8 -*-

import os
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import time

dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = ("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36")


driver = webdriver.PhantomJS(desired_capabilities=dcap, service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any', '--web-security=false'])
driver.set_window_size(1024, 768)
driver.get('http://www.httpbin.org')
time.sleep(2)
driver.execute_script('document.getElementsByClassName("mp")[0].style.background = "green"')
#driver.execute_script('document.body.style.background = "black"')
driver.save_screenshot('testing1.png')

driver.get('http://www.kahoot.it')
time.sleep(2)
driver.execute_script("var body = document.getElementsByTagName('body')[0]; body.setAttribute('background-color', 'white')")
driver.execute_script('document.body.style.background = "black"')
driver.save_screenshot('testing2.png')

As a suggestion for other transparent issues if you don't wanna look for DOM items, just convert your png to jpg using Image class for python and every transparent pixel would be set to white. 如果您不想查找DOM项目,建议其他透明问题,只需使用Image类为python将您的png转换为jpg,并将每个透明像素设置为白色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM