How to solve the screenshot U made by using PhantomJS+Selenium+Python is always transparent or black(looks like)

in #phantomjs7 years ago (edited)

Environment

  • Ubuntu 16.04 X64 Desktop in Vmware
  • PhantomJS 2.1.1
  • Python3

Phenomenon

With following code,

from core import helper
from selenium import webdriver
driver = webdriver.PhantomJS(executable_path=helper.CURR_PATH + '/core/phantomjs-2.1.1')
driver.get('http://phantomjs.org/download.html')
driver.implicitly_wait(20)
driver.save_screenshot('3.png')
driver.quit()

i always got such image as below you can see,
sc.PNG
which is because its background is transparent, although it looks like in black.

Solution

Right after you invoke the method of driver.get() as above,
just inject one line,

driver.execute_script('document.body.style.background = "white"')

for me , such approach works well.

Reference