一个更简单的解决浏览器不安全链接的方法
前面讲到可以通过创建单独的Firefox配置,每次实例Webdriver.Firefox()时加载指定的配置来解决浏览器不安全链接,但只适用于本地,不方便迁移以及分布式使用。
下面介绍个更方便的办法:
from selenium.webdriver import Remote
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
def browser():
selenium_grid_url = "127.0.0.1:4444/wd/hub"
capabilities = DesiredCapabilities.FIREFOX.copy()
driver = Remote(command_executor = selenium_grid_url , desired_capabilities = capabilities)
return driver
看下desired_capabilities 的源码
class DesiredCapabilities(object):
FIREFOX = {
"browserName": "firefox",
"marionette": True,
"acceptInsecureCerts": True,
}
INTERNETEXPLORER = {
"browserName": "internet explorer",
"version": "",
"platform": "WINDOWS",
}
EDGE = {
"browserName": "MicrosoftEdge",
"version": "",
"platform": "WINDOWS"
}
CHROME = {
"browserName": "chrome",
"version": "",
"platform": "ANY",
}
Firefox 中的 “acceptInsecureCerts”: True 表示接收不安全链接,因此直接DesiredCapabilities.FIREFOX.copy()不需要任何配置,即可解决
如果需要应用到Chrome中,因为字典中默认没有acceptInsecureCerts键值,需要额外添加如下:
from selenium.webdriver import Remote
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
def browser():
selenium_grid_url = "127.0.0.1:4444/wd/hub"
capabilities = DesiredCapabilities.CHROME.copy()
capabilities['acceptInsecureCerts'] = True
driver = Remote(command_executor = selenium_grid_url , desired_capabilities = capabilities)
return driver
其它浏览器以此类推,以上代码运行前需要启动seleniumServer(selenium-server-standalone-3.141.59.jar)
java -jar path\selenium-server-standalone-3.141.59.jar