python selenium flash_Selenium + chromeDriver + Python3 完成 Flash 播放

在使用 selenium + chromeDriver + python3 截图时,遇上 Flash 无法加载,导致了截图 Falsh 是空白区。

环境要求:selenium chromeDriver Python3

问题

chrome 无头浏览器无法自动加载 Flash

解决办法

参考了 allow-flash-content-in-chrome-69-running-via-chromedriver 的回答,直接修改 Chrome 的设置 chrome://settings/content/siteDetails?site= 里面的 Flash 设置,修改为 Allow

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import Select

class chromeDriver():

def __init__(self, driver = ''):

# 设置窗口大小

self.window_width = 1680

self.window_height = 948

# 设置 chromedriver 位置

self.executable_path = '/usr/local/bin/chromedriver'

# 设置 Flash 的路径

self.flash_path = '/Users/cindy/Library/Application Support/Google/Chrome/PepperFlash/32.0.0.171/PepperFlashPlayer.plugin'

# 获取 driver

if driver: self.driver = driver

else:

self.driver = self.get_chrome_driver()

def get_chrome_driver(self):

# 头部

user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'

# 创建参数对象

options = webdriver.ChromeOptions()

prefs = {

# 开启图片

"profile.managed_default_content_settings.images":1,

# 关闭 Notification

"profile.default_content_setting_values.notifications": 2,

}

# 设置 Flash 的路径

options.add_argument('--ppapi-flash-version=32.0.0.171')

options.add_argument('--ppapi-flash-path=' + self.flash_path)

options.add_argument('binary_location=/Applications/Google Chrome.app/Contents/MacOS/Google Chrome')

# 指定屏幕分辨率

options.add_argument('window-size=' + str(self.window_width) + 'x' + str(self.window_height) + '\'')

# 最大化窗口

options.add_argument('--start-maximized')

# 规避bug

options.add_argument('--disable-gpu')

# 禁用弹出拦截

options.add_argument('--disable-popup-blocking')

# 隐藏自动软件

options.add_argument('disable-infobars')

# 设置中文

options.add_argument('lang=zh_CN.UTF-8')

#忽略 Chrome 浏览器证书错误报警提示

options.add_argument('--ignore-certificate-errors')

# 更换头部

options.add_argument('user-agent=' + user_agent)

options.add_argument('no-default-browser-check')

# 关闭特征变量

options.add_experimental_option('excludeSwitches', ['enable-automation'])

options.add_experimental_option('prefs', prefs)

# 创建 Chrome 对象

driver = webdriver.Chrome(options = options, executable_path = self.executable_path)

return driver

def get(self, web_url):

if not web_url: return False

return self.driver.get(web_url)

def add_flash_site(self, web_url):

if not web_url: return False

self.get("chrome://settings/content/siteDetails?site=" + web_url)

root1 = self.driver.find_element(By.TAG_NAME, "settings-ui")

shadow_root1 = self.expand_root_element(root1)

root2 = shadow_root1.find_element(By.ID, "container")

root3 = root2.find_element(By.ID, "main")

shadow_root3 = self.expand_root_element(root3)

shadow_root3 = self.expand_root_element(root3)

root4 = shadow_root3.find_element(By.CLASS_NAME, "showing-subpage")

shadow_root4 = self.expand_root_element(root4)

root5 = shadow_root4.find_element(By.ID, "advancedPage")

root6 = root5.find_element(By.TAG_NAME, "settings-privacy-page")

shadow_root6 = self.expand_root_element(root6)

root7 = shadow_root6.find_element(By.ID, "pages")

root8 = root7.find_element(By.TAG_NAME, "settings-subpage")

root9 = root8.find_element(By.TAG_NAME, "site-details")

shadow_root9 = self.expand_root_element(root9)

root10 = shadow_root9.find_element(By.ID, "plugins")

shadow_root10 = self.expand_root_element(root10)

root11 = shadow_root10.find_element(By.ID, "permission")

Select(root11).select_by_value("allow")

def expand_root_element(self, element):

return self.driver.execute_script("return arguments[0].shadowRoot", element)

def get_flash_url(self, web_url):

if not web_url: return False

self.add_flash_site(web_url)

self.get(web_url)

def quit_driver(self):

self.driver.quit()

driver = chromeDriver()

url = 'http://your.website/'

driver.get_flash_url(url)

最后

不能使用无界面模式,不能设置 handless 参数 options.add_argument('--headless')。否则无法直接修改 Chrome 的设置。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值