selenium chrome摄像头、麦克、通知

selenium chrome浏览器权限管理

环境:

selenium version:3

python3 version:3.8

platform:ubuntu 20.04

方式一

方法:启动chrome浏览器时添加参数

prefs = {
        "profile.default_content_setting_values.notifications": 1,
        "profile.default_content_setting_values.media_stream_mic": 1,
        "profile.default_content_setting_values.media_stream_camera": 1
        }

说明:

  1.  1表示allow

  2.  2表示block

  3. notifications 通知

  4. media_stream_mic 麦克风

  5. media_stream_camera 摄像头

案例:

url = "https://www.baidu.com"
prefs = {
        "profile.default_content_setting_values.notifications": 1,
        "profile.default_content_setting_values.media_stream_mic": 1,
        "profile.default_content_setting_values.media_stream_camera": 1
    }
options = Options()
options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(options=options)
driver.get(url)

注意:

  1. 目前不能使用--remote-debugging-port=9222已经启动的浏览器,否则报错。

  报错内容:

Message: invalid argument: cannot parse capability: goog:chromeOptions
from invalid argument: unrecognized chrome option: prefs

  关联网站:https://stackoverflow.com/questions/63091842/cannot-parse-capability-googchromeoptions-from-invalid-argument-unrecognized

方式二

方法:chrome的设置界面全是shadow-root,需要一层一层查早。

案例:enble flash

from selenium import webdriver
from selenium.webdriver.support.select import Select


options = Options()
chromedriver = "/usr/local/chromedriver/chromedriver"
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_argument("--user-data-dir=/home/chrome/.config/google-chrome/")
url = "chrome://settings/content/siteDetails?site=https%3A%2F%2Fwww.baidu.com%2F"
driver = webdriver.Chrome(chromedriver, options=options)

def expand_root_element(element):
    return driver.execute_script("return argument[0].shadowRoor", element)

def site_flash(url):
    driver.get(url)
    root1 = driver.find_element(By.TAG_NAME, "settings-ui")
    shadow_root1 = expand_root_element(root1)
    root2 = shadow_root1.find_element(By.ID, "container")
    root3 = root2.find_element(By.ID, "main")
    shadow_root3 = expand_root_element(root3)
    root4 = shadow_root3.find_element(By.CLASS_NAME, "showing-subpage")
    shadow_root4 = expand_root_element(root4)
    root5 = shadow_root4.find_element(By.ID, "advancedPage")
    root6 = root5.find_element(By.TAG_NAME, "settings-privacy-page")
    shadow_root6 = 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 = expand_root_element(root9)
    root10 = shadow_root9.find_element(By.ID, "plugins")
    shadow_root10 = expand_root_element(root10)
    root11 = shadow_root10.find_element(By.ID, "permission")
    Select(root11).select_by_value("allow")
    driver.close()
    
site_flash(url)

注意:

  1. 不能使用无界面模式。不能设置 handless 参数 。否则无法直接修改 Chrome 的设置

方式三

方法:使用键盘

案例:block 通知

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options


options = Options()
chromedriver = "/usr/local/chromedriver/chromedriver"
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_argument("--user-data-dir=/home/chrome/.config/google-chrome/")
url = "chrome://settings/content/siteDetails?site=https%3A%2F%2Fwww.baidu.com%2F"
driver = webdriver.Chrome(chromedriver, options=options)

driver.get(url)
root1 = driver.find_element(By.TAG_NAME, "settings-ui")
for i in range(7):
    root1.send_keys(Keys.TAB)
root1.send_keys(Keys.ENTER)
root1.send_keys(Keys.DOWN)
root1.send_keys(Keys.ENTER)

注意:

  1. 不能后台运行

参考:

https://www.ucloud.cn/yun/43636.html

https://blog.51cto.com/u_15047490/2560925

https://stackoverflow.com/questions/52185371/allow-flash-content-in-chrome-69-running-via-chromedriver

https://blog.csdn.net/yjxtor/article/details/109386396

 

posted @ 2022-08-01 15:24  zhuang6  阅读(1509)  评论(0)    收藏  举报