Selenium 启动默认配置浏览器 (允许摄像头权限)

匿名 · 2018年12月06日 · 2837 次阅读

启动默认配置浏览器 (允许摄像头权限)

自动化测试时经常遇见打开浏览器时,需要点击摄像头权限等,以下有几种处理方式

  • Chrome 读取配置文件允许摄像头权限

    chrome_driver='/Users/lisen/Desktop/chromedriver'
    os.environ["webdriver.Chrome.driver"]=chrome_driver
    option = webdriver.ChromeOptions()
    option.add_argument("--user-data-dir=/Users/lisen/Library/Application Support/Google/Chrome")
    driver = webdriver.Chrome(executable_path=chrome_driver, chrome_options=option)
    
  • Chrome 设置 profile 允许摄像头权限

    chrome_driver='/Users/lisen/Desktop/chromedriver'
    os.environ["webdriver.Chrome.driver"]=chrome_driver
    option = webdriver.ChromeOptions()
    prefs = {'profile.default_content_setting_values.media_stream_camera': 1,
         'profile.default_content_setting_values.media_stream_mic': 1,
         'profile.default_content_setting_values.notifications': 1,
         'profile.default_content_setting_values.geolocation': 1}
    option.add_experimental_option('prefs', prefs)
    driver = webdriver.Chrome(executable_path=chrome_driver,chrome_options=option)
    
  • Firefox 读取配置文件允许摄像头权限

    firefox_driver='/Users/lisen/Desktop/geckodriver57+'
    MacFirefoxPath="/Users/lisen/Library/Application Support/Firefox/Profiles/28mwdrcp.default"
    os.environ["webdriver.Firefox.driver"]=firefox_driver
    firefoxProfile = webdriver.FirefoxProfile(MacFirefoxPath)
    driver = webdriver.Firefox(executable_path=firefox_driver,firefox_profile=firefoxProfile)
    
  • Firefox 设置 profile 允许摄像头权限

    firefox_driver='/Users/lisen/Desktop/geckodriver63'
    os.environ["webdriver.Firefox.driver"]=firefox_driver
    profile = webdriver.FirefoxProfile()
    profile.set_preference ('media.navigator.permission.disabled', True)
    profile.update_preferences()
    driver = webdriver.Firefox(executable_path=firefox_driver,firefox_profile=profile)
    

    浏览器兼容测试 Demo:https://github.com/lisen886/Good-name-you-remember-me/tree/master/compatibilityTest 个人瞎捣鼓,欢迎大佬随时纠正

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册