zoukankan      html  css  js  c++  java
  • python selenium

    在实际的自动化测试实践中,因为越来越多的站点接入https,使得我们原有的python selenium2自动化测试代码进行测试时,浏览器总是报安全问题,即便在浏览器选项中将被测网址加入信任网址也没用。

    一般情况下,我们访问http站点时的代码如下:

    driver = webdriver.Firefox()

    driver.get(u'http://www.testingunion.com')

    一般情况下,这样处理是正常, 但如果目标url是HTTPS访问模式,则浏览器会提示安全问题或是非信任站点。

    在不同的浏览器上显示的提示如图所示(这里以英文版的浏览器为准):


    浏览器SSL提示

    我们看一下IE的解决方案,对ie浏览器而言,需要添加Desired Capabilities的acceptSslCerts选项为True,代码如下:

    #_*_ coding:utf-8 _*_

    __author__ = '苦叶子'

    from selenium import webdriver

    if __name__ == '__main__':

        capabilities = webdriver.DesiredCapabilities().INTERNETEXPLORER

        capabilities['acceptSslCerts'] = True

        driver = webdriver.Ie(capabilities=capabilities)

        driver.get(u'https://cacert.org/')

        print driver.title

        driver.quit()

    对于firefox浏览器则需要添加FirefoxProfile()的accept_untrusted_certs的选项为True,示例代码如下:

    #_*_ coding:utf-8 _*_

    __author__ = '苦叶子'

    from selenium import webdriver

    if __name__ == '__main__':   

        profile=webdriver.FirefoxProfile()

        profile.accept_untrusted_certs=True

        driver=webdriver.Firefox(firefox_profile=profile)

        driver.get(u'https://cacert.org/')

        driver.close()

    对于chrome浏览器则需要添加ChromeOptions()的--ignore-certificate-errors选项为True,示例代码如下:

    #_*_ coding:utf-8 _*_

    __author__ = '苦叶子'

    from selenium import webdriver

    if __name__ == '__main__':

        options=webdriver.ChromeOptions()

        options.add_argument('--ignore-certificate-errors')

        driver=webdriver.Chrome(chrome_options=options)

        driver.get(u'https://cacert.org/')

        driver.close()

    结束语

        对于在利用上述方式针对不同浏览器处理SSL时,可能还会碰到还是处理不了的情况,比如提示证书损坏、无效等等;如果出现这类情况,请联系网站管理员更新SSL证书。

  • 相关阅读:
    VS2013安装与部署工具中添加 vcredist_x64.exe
    ZeroMQ高水位标记(high-water mark,HWM)
    Ubuntu16.04 动态链接库(.so)查询路径设置
    ubuntu16.04开机启动字符界面
    python 全局搜索路径
    learning to rank
    数据集
    hadoop streaming 文档
    机器学习:一些感想
    矩阵分解 推荐
  • 原文地址:https://www.cnblogs.com/alamZ/p/6932913.html
Copyright © 2011-2022 走看看