python selenium chrome control devtools some clues

  1. chrome webdriver download address: http://chromedriver.storage.googleapis.com/index.html
    Note that it corresponds to the kernel version of the domestic browser (the Wish browser 80.0.3987.163 kernel can use 80.0.3987.106 webdriver)
    as long as the browser’s The kernel version is not lower than the version of webdriver. The minimum version on Nuget is 71, so QQ browser cannot be controlled by C#, and QQ only has version 70 kernel.
  2. If you want to open the browser first and then use selenium to control, you need to add startup parameters to the browser:
twinkstar.exe --remote-debugging-port=9222
  1. Python calls the initialization of the existing Star Wish browser
from selenium import webdriver
options = webdriver.ChromeOptions()
options.debugger_address = "127.0.0.1:9222"
driver = webdriver.Chrome(options=options)
  1. Use the built-in screenshot function of devtools! (Supported by kernel 59, see https://developers.google.com/web/updates/2017/04/devtools-release-notes for details )
import base64
res = driver.execute_cdp_cmd('Page.captureScreenshot', {
    
    })
 
with open('hao123.png', 'wb') as f:
    img = base64.b64decode(res['data'])
    f.write(img)
  1. The JS native method of Flash zooming is Zoom(percentage)where percentagethe reciprocal of the magnification is multiplied by 100. For example, zooming 4 times is 1/4*100=25.
  2. After Flash zooming, the JS native method to move the field of view is Pan(x_position,y_position,mode).
    x_positionThe parameter is the number of moving abscissas, and the negative
    y_positionparameter is the number of moving ordinates. The negative
    modeparameter is temporarily unclear. First fill in 1 to indicate the unit of coordinates, but when the value is "0", the number of pixels is the unit When it is "1", the unit is percentage.
  3. The JS native method to control the transparency of the entire Flash.
    TSetProperty("_root",6,50)
    The last parameter is the opacity from 0 to 100, 0 means no display, and 100 means completely opaque.
  4. The CDP command to set the analog device is
'Emulation.setDeviceMetricsOverride', {
    
    
  mobile: true,
  width: 412,
  height: 732,
  deviceScaleFactor: 2.625,
}
  1. The command for python selenium to specify the interception area is
clip = driver.execute_script('return {x: 0, y: 0, width: 4320, height: 7680, scale: 1};', '') # 模拟的是竖屏
res = driver.execute_cdp_cmd('Page.captureScreenshot', {
    
    'clip': clip})
  1. In summary, the operations that python needs to perform are (cut the high-definition picture of the Xiaohuaxian page tour selection interface (the one in the middle))
from selenium import webdriver
import base64

# 接入既有的浏览器进程
options = webdriver.ChromeOptions()
options.debugger_address = "127.0.0.1:9222"
driver = webdriver.Chrome(options=options)

# 直接开启设备模拟,不要再手动开devtools了,否则截图截的是devtools的界面!
driver.execute_cdp_cmd('Emulation.setDeviceMetricsOverride', {
    
    'mobile':False, 'width':4320, 'height':7680, 'deviceScaleFactor': 1})
# 缩放Flash
driver.execute_script('document.getElementsByTagName("embed")[0].Zoom(100)')
driver.execute_script('document.getElementsByTagName("embed")[0].Zoom(25)')
# Flash缩放后的视野位置微调,还需要完善
driver.execute_script('document.getElementsByTagName("embed")[0].Pan(0,-300,0)') 
# 执行截图
res = driver.execute_cdp_cmd('Page.captureScreenshot', {
    
     'fromSurface': True})
# 返回的base64内容写入PNG文件
with open('hao123.png', 'wb') as f:
    img = base64.b64decode(res['data'])
    f.write(img)

# 关闭设备模拟
driver.execute_cdp_cmd('Emulation.clearDeviceMetricsOverride', {
    
    })
# Flash缩放复原
driver.execute_script('document.getElementsByTagName("embed")[0].Zoom(500)')

Guess you like

Origin blog.csdn.net/qq_35977139/article/details/111399633