Selenium+Chrome浏览器自动加载Flash

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

Selenium+Chrome浏览器自动加载Flash

在自动化脚本编写过程中,遇到一些网页需要使用Flash插件,但是通过Selenium启动的浏览器不能默认对网页启动Flash,需要在chrome://settings/content/siteDetails?site={url}网页进行设置。

以 Python 为例,其他 语言 书写想法和步骤一样,作为参考。

注意:使用的是 Python 3.6.4 版本。
注意:使用 selenium 时需要再 python 安装目录放入:chromedriver.exe。

chromedriver 版本需要和 chrome 浏览器版本一致。

查看 chrome 版本:chrome://version

chromedriver 下载地址:http://npm.taobao.org/mirrors/chromedriver

在这里插入图片描述


添加 argument:–allow-outdated-plugins
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

# 启动浏览器,打开 demo 演示页面
chrome_options = Options()
chrome_options.add_argument('--allow-outdated-plugins')
driver = webdriver.Chrome(options=chrome_options)
driver.get('https://demo.imacros.net/automate/flashdemo ')

–allow-outdated-plugins 参数作用:允许使用旧版本的插件。
注意:旧版本的插件可能有漏洞,不要访问来历不明的网站。

允许Falsh方法
Chrome73.0.3683.86 版本如下:
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):
    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")


设置driver允许Flash
allow_flash(driver,"http://testops:81")
解释

shadow root elements
该种类elements下的元素,不能通过selenium方法直接定位,需要JS的方法:element.shadowRoot 找到元素

版本

在Chrome73.0.3683.86上测试成功,旧版本的Chrome不一定需要这么麻烦,添加一个argument也许就可以了。


完整版本的代码测试:

Chrome87.0.4280.88 版本如下:亲测可用!

flash demo 演示网站: https://demo.imacros.net/automate/flashdemo

# 注意 python3 以上才有 2,3行代码。
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


from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# 获取本地 chrome 参数
chrome_options = Options()
chrome_options.add_argument('--allow-outdated-plugins')
driver = webdriver.Chrome(options=chrome_options)

# 总是允许 flash 方法。
def allow_flash(driver, url):
    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, "basicPage")
    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.CSS_SELECTOR, "div.list-frame > site-details-permission:nth-child(7)")

    shadow_root10 = _shadow_root(driver, root10)

    root11 = shadow_root10.find_element(WebBy.ID, "permission")
    WebSelect(root11).select_by_value("allow")

allow_flash(driver,"https://demo.imacros.net")

# 打开带有 flash 的测试页面。
driver.get('https://demo.imacros.net/automate/flashdemo')

备注:不同版本的浏览器,可能元素所在的位置不同,只需要在获取的时候注意一下,修改几个 doc.find_element() 即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值