使用python中Selenium和谷歌浏览器Chrome加载flash

用到在网页中自动播放flash,注意chrome版本和chromedrive的版本问题。

解决python引用谷歌浏览器的chromedrive测试网页。实现自动化的事情,需要自动播放flash。由于谷歌浏览器开始逐步放弃flash,并不在默认支持播放,为了在自动化测试中减少手动操作的步骤,需要自动播放flash,以下代码可行。

直接上代码吧

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python 
# -*- coding:utf-8 -*-
# @Time : 2019-12-29 19:00
# @Author : Greedyboy
# @Email : greedyboy@163.com
# @File : jxjy_test.py
# @Software: PyCharm
"""info

"""
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--allow-outdated-plugins')
driver = webdriver.Chrome(options=chrome_options)

from urllib.parse import quote_plus as url_quoteplus
from urllib.parse import urlsplit
from selenium.webdriver.common.by import By as WebBy
from selenium.webdriver.support.ui import Select as WebSelect

def allow_flash(driver, url):
#允许使用flash
def _base_url(url):
if url.find("://") == -1:
url = "http://{}".format(url)
urls = urlsplit(url)
return "{}://{}".format(urls.scheme, urls.netloc)

def _shadow_root(driver, element):
return driver.execute_script("return arguments[0].shadowRoot", element)

base_url = _base_url(url)
driver.get("chrome://settings/content/siteDetails?site={}".format(url_quoteplus(base_url)))

root1 = driver.find_element(WebBy.TAG_NAME, "settings-ui")
shadow_root1 = _shadow_root(driver, root1)
root2 = shadow_root1.find_element(WebBy.ID, "container")
root3 = root2.find_element(WebBy.ID, "main")
shadow_root3 = _shadow_root(driver, root3)
root4 = shadow_root3.find_element(WebBy.CLASS_NAME, "showing-subpage")
shadow_root4 = _shadow_root(driver, root4)
root5 = shadow_root4.find_element(WebBy.ID, "advancedPage")
root6 = root5.find_element(WebBy.TAG_NAME, "settings-privacy-page")
shadow_root6 = _shadow_root(driver, root6)
root7 = shadow_root6.find_element(WebBy.ID, "pages")
root8 = root7.find_element(WebBy.TAG_NAME, "settings-subpage")
root9 = root8.find_element(WebBy.TAG_NAME, "site-details")
shadow_root9 = _shadow_root(driver, root9)
root10 = shadow_root9.find_element(WebBy.ID, "plugins") # Flash
shadow_root10 = _shadow_root(driver, root10)
root11 = shadow_root10.find_element(WebBy.ID, "permission")
WebSelect(root11).select_by_value("allow")

def openweb(url,driver=driver,isflash=True):
allow_flash(driver, "http://xz2.jinlingshuo.com:8886/")
driver.get(url)


if __name__ == "__main__":
# driver.get('http://xz2.jinlingshuo.com:8886/')
# allow_flash(driver, "http://xz2.jinlingshuo.com:8886/")
# driver.get('http://xz2.jinlingshuo.com:8886/')
# driver.close()
url='http://xz2.jinlingshuo.com:8886/'
openweb(url)

pass

饮水思源

https://blog.csdn.net/weixin_38389124/article/details/88894746