Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. Any clue why? It works with pages such as Google.com, but not kahoot.it, the one I want. It also works with everything else I need in Firefox, but not in 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! :)

like image 530
Morten Stulen Avatar asked Nov 25 '14 00:11

Morten Stulen


1 Answers

Your problem is about your website.

You typed it as http://www.kahoot.it but in the end, it does redirect to https site. So, your PhantomJS is getting errors from ssl version or ssl itself if where errors.

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.

On the other hand, if you don't like transparent background set your own with: 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. 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.

like image 182
m3nda Avatar answered Oct 21 '22 08:10

m3nda