ztf.net.cn
七牛云
您当前的位置: 首页 >  个人博客

selenium SSL证书验证和参数解释


Selenium是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。本文介绍了各种参数的用法,并提供了SSL证书验证的解决方案。



本文内容:

1、14中参数案例及解释

2、SSL证书验证的三种解决方案


参数

chrome_options.add_argument('--headless') # 无头模式,可不启用界面显示运行

chrome_options.add_argument('--disable-gpu') # 禁用GPU加速

chrome_options.add_argument('--start-maximized')#浏览器最大化

chrome_options.add_argument('--window-size=1280x1024') # 设置浏览器分辨率(窗口大小)

chrome_options.add_argument('log-level=3')

chrome_options.add_argument('--user-agent=""') # 设置请求头的User-Agent

chrome_options.add_argument('--disable-infobars') # 禁用浏览器正在被自动化程序控制的提示

chrome_options.add_argument('--incognito') # 隐身模式(无痕模式)

chrome_options.add_argument('--hide-scrollbars') # 隐藏滚动条, 应对一些特殊页面

chrome_options.add_argument('--disable-javascript') # 禁用javascript

chrome_options.add_argument('--blink-settings=imagesEnabled=false') # 不加载图片, 提升速度

chrome_options.add_argument('--ignore-certificate-errors') # 禁用扩展插件并实现窗口最大化

chrome_options.add_argument('–disable-software-rasterizer')

chrome_options.add_argument('--disable-extensions')


    ch_options = webdriver.ChromeOptions()

    # 不加载图片,加快访问速度
    ch_options.add_experimental_option("prefs", {"profile.mamaged_default_content_settings.images": 2})

    # 此步骤很重要,设置为开发者模式,防止被各大网站识别出来使用了Selenium
    ch_options.add_experimental_option('excludeSwitches', ['enable-automation'])
    # ch_options.add_experimental_option("debuggerAddress", "127.0.0.1:9999")
    ch_options.add_argument('--proxy--server=127.0.0.1:8080')
    ch_options.add_argument('--disable-infobars')  # 禁用浏览器正在被自动化程序控制的提示
    ch_options.add_argument('--incognito')
    browser = webdriver.Chrome(options=ch_options)


How to handle this error in Selenium?

There are many different ways we can solve this problem and the solution depends on the browser.

Firefox Browser – Method 1:

You can set ‘accept_untrusted_certs‘ value in FirefoxProfile() option to True.

from selenium import webdriver

profile = webdriver.FirefoxProfile()
profile.accept_untrusted_certs = True

driver = webdriver.Firefox(firefox_profile=profile)
driver.get('https://url')
print(driver.title)
driver.close()

Firefox Browser – Method 2:

You could use DesiredCapabilities to set ‘acceptInsecureCerts‘ capability to True (Note: This should work for all browsers since it is a generic read/write capability)

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

desired_capabilities = DesiredCapabilities.FIREFOX.copy()
desired_capabilities['acceptInsecureCerts'] = True

driver = webdriver.Firefox(capabilities=desired_capabilities)
driver.get('https://url')
print(driver.title)
driver.close()

Chrome Browser – Method 1:

You can add ‘–ignore-certificate-errors‘ and ‘–allow-running-insecure-content‘ arguments to Chrome Options().

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument('--allow-running-insecure-content')
options.add_argument('--ignore-certificate-errors')

driver = webdriver.Chrome(chrome_options=options)
driver.get('https://url')
print(driver.title)
driver.close()


Chrome Browser – Method 2:

You could use DesiredCapabilities to set ‘acceptInsecureCerts‘ capability to True (Note: This should work for all browsers since it is a generic read/write capability)

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.set_capability("acceptInsecureCerts", True)

driver = webdriver.Chrome(chrome_options=options)
driver.get('https://url')
print(driver.title)
driver.close()

Chrome Browser – Method 3:

This is also using DesiredCapabilities again; But we are going to use Options -> set_capability() method to set ‘acceptInsecureCerts‘ to True.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.set_capability("acceptInsecureCerts", True)

driver = webdriver.Chrome(chrome_options=options)
driver.get('https://url')
print(driver.title)
driver.close()
ZTF

ZTF|时间:2022-10-09

如果缘分安排我们相遇,请不要让她擦肩而过。扫一扫二维码,加我为好友吧!
标签云