python selenium2示例 - SSL处理(转载)

前言

随着现在站点对安全的要求越来越高,越来越多的企业网站接入了https,随着https的大规模应用,我们在使用python selenium2进行自动化测试时,也要面临的挑战。


面临的问题

在实际的自动化测试实践中,因为越来越多的站点接入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()
可以使用PythonSelenium库来清理谷歌浏览器缓存。具体步骤如下: 1. 首先需要导入Selenium库和webdriver模块,以及ChromeOptions类和Options类。 ```python from selenium import webdriver from selenium.webdriver.chrome.options import Options ``` 2. 创建一个ChromeOptions对象,并设置浏览器参数。 ```python options = Options() options.add_argument('--ignore-certificate-errors') options.add_argument('--ignore-ssl-errors') ``` 3. 创建一个WebDriver对象,并使用ChromeOptions对象作为参数来设置浏览器参数。 ```python driver = webdriver.Chrome(chrome_options=options) ``` 4. 打开浏览器,并访问需要清理缓存的网站。 ```python driver.get('https://www.example.com') ``` 5. 使用execute_script()方法执行JavaScript代码来清理缓存。 ```python driver.execute_script('window.localStorage.clear();') driver.execute_script('window.sessionStorage.clear();') driver.execute_script('window.location.reload();') ``` 6. 关闭浏览器。 ```python driver.quit() ``` 完整的代码示例: ```python from selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() options.add_argument('--ignore-certificate-errors') options.add_argument('--ignore-ssl-errors') driver = webdriver.Chrome(chrome_options=options) driver.get('https://www.example.com') driver.execute_script('window.localStorage.clear();') driver.execute_script('window.sessionStorage.clear();') driver.execute_script('window.location.reload();') driver.quit() ``` 注意:这个方法只能清理浏览器缓存,不能清理浏览器历史记录和cookie等其他数据。如果需要清理其他数据,可以使用selenium的delete_all_cookies()方法来删除cookie。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值