Transparent screenshot with Firefox selenium

0

In short

I want to take a screenshot of an element without the background with selenium.webdriver.Firefox

Actual behaviour

Actual behaviour

Expected / desired behaviour

enter image description here

My code

from selenium import webdriver
from PIL import Image

CSS_SELECTOR = ".RNNXgb"

driver = webdriver.Firefox()
driver.get("https://google.com")
driver.find_element_by_css_selector(CSS_SELECTOR)

image = Image.open(io.BytesIO(element.screenshot_as_png))
image.save("myscreenshot.png", "png")

driver.quit()

Things not working (not suitable for this situation)

Replacing white with transparent pixels

This does not work because it replaces the white in the element too

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)
image = replace_color(image, 0xffffffff)

Settings trasparent background

It does nothing

driver.execute_script("document.getElementById('hplogo').style.background = 'transparent'")

Use of DevTools API

Only for Chromium, not Firefox

python
python-3.x
selenium
selenium-firefoxdriver
asked on Stack Overflow Jul 24, 2020 by GodSaveTheDoge • edited Jul 24, 2020 by GodSaveTheDoge

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0