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

Transparent screenshot with PhantomJS in Selenium [Python]?


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 osimport seleniumfrom selenium import webdriverfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.common.desired_capabilities import DesiredCapabilitiesimport timedcap = 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.