Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting chrome devtool's "emulation/default-background-color-override" #359

Closed
carlhu opened this issue Jan 6, 2021 · 1 comment · Fixed by #459
Closed

Setting chrome devtool's "emulation/default-background-color-override" #359

carlhu opened this issue Jan 6, 2021 · 1 comment · Fixed by #459

Comments

@carlhu
Copy link

carlhu commented Jan 6, 2021

Etaoin's api is beautifully designed: thank you so much for sharing this.

I'd like to use the api's element capture api (which works great) except with transparent background. When I use the devtool protocol directly, setting the emulation/set-default-background-color-override to some color with alpha 0 works great. But I can't figure out how to use etaoin to do execute a (in Selenium's terms) a cdp command. Can you offer any guidance?

If it helps, here is the java from their docs:

driver.executeCdpCommand("Emulation.setDefaultBackgroundColorOverride", color);
@lread
Copy link
Collaborator

lread commented May 9, 2022

@carlhu, I too am enjoying Etaoin (thanks @igrishaev!).

And I too was wondering how to take a screenshot with a transparent background.
I searched around and found this Python example on StackOverflow:

from selenium import webdriver
import json

def send(cmd, params={}):
  resource = "/session/%s/chromium/send_command_and_get_result" % driver.session_id
  url = driver.command_executor._url + resource
  body = json.dumps({'cmd':cmd, 'params': params})
  response = driver.command_executor._request('POST', url, body)
  if response['status']: raise Exception(response.get('value'))
  return response.get('value')

options = webdriver.ChromeOptions()
options.add_argument("disable-gpu")
options.add_argument("disable-infobars")

driver = webdriver.Chrome(chrome_options=options)
driver.get("http://www.bbc.co.uk/news")

# take screenshot with a transparent background
send("Emulation.setDefaultBackgroundColorOverride", {'color': {'r': 0, 'g': 0, 'b': 0, 'a': 0}})
driver.get_screenshot_as_file(r"C:\temp\screenshot.png")
send("Emulation.setDefaultBackgroundColorOverride")  # restore

The etaion API includes an execute, so I had it do what I found to be the interesting parts of the above Python like so:

(etaoin/execute {:driver driver
                 :method :post
                 :path [:session (:session driver) "chromium" "send_command_and_get_result"]
                 :data {:cmd "Emulation.setDefaultBackgroundColorOverride"
                        :params {:color {:r 0 :g 0 :b 0 :a 0}}}})

Which I follow with my particular screenshot request:

(etaoin/screenshot-element driver
                           {:tag :div :class :wrapper}
                           (str target-dir "/" github-id ".png"))

And it seems to work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants