Selenium execution cdp command, driver.execute_cdp_cmd usage

Chrome comes with developer tools DevTools very powerful. Sometimes we use Selenium operation browser settings need to complete some calls about DevTools methods, such as simulated mobile devices, network simulation, and so weak.
Selenium is WebDriver class has a execute_cdp_cmd(self, cmd, cmd_args)method that can be used to perform the Chrome Developer Tools command.

cdp namely Chrome DevTools Protocal, Chrome developer tools agreement, API documentation, refer to: https://chromedevtools.github.io/devtools-protocol/tot/Emulation

Use, examples:

import requests
from selenium import webdriver
from time import sleep
import base64

driver = webdriver.Chrome()
driver.get('https://www.hao123.com/')

res = driver.execute_cdp_cmd('Page.captureScreenshot', {})

with open('hao123.png', 'wb') as f:
    img = base64.b64decode(res['data'])
    f.write(img)

sleep(3)
driver.quit()

This example is the method of Theme call Page class CDP, png support and a jpeg format, returning to the calling data is base64 encoded data field of the picture, the decoded encoded in a binary image can be kept.

I regret that, although Chrome Developer Tools menu Front Command has Capture full screenshot of the command, as shown below, but the method cdp's no such method, after Selenium3 all browsers are not full-screen screenshots.

Guess you like

Origin www.cnblogs.com/superhin/p/11481910.html