1. 使用chrome时
from selenium import webdriver options = webdriver.ChromeOptions() options.add_argument('ignore-certificate-errors') browser = webdriver.Chrome(chrome_options=options) # 新版本用options参数,chrome_options即将弃用 browser.get("https://10.5.16.1:8344/") print(browser.current_url) print(browser.page_source)
webdriver下载地址: http://npm.taobao.org/mirrors/chromedriver,下载自己对应浏览器版本的软件
下载后解压到自己python.exe所在的文件夹,比如我的位置为D:Program Filespython
更多参数设置可参考: https://www.cnblogs.com/andy0816/p/12727392.html
2. 使用phantomJS时
from selenium import webdriver browser=webdriver.PhantomJS(service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any']) browser.get("https://10.5.16.1:8344") print(browser.current_url)
因为Https安全协议的问题,比如协议tlsv1.0 tlsv1.2等,会导致访问时出现访问页面不安全的界面
可以给phantomjs设置忽略ssl证书的错误 --ignore-ssl-errors=true,以及协议版本为任何版本--ssl-protocol=any
更多参数设置可参考:https://www.cnblogs.com/fly-kaka/p/6656196.html
# coding=utf-8 import time from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.support.ui import WebDriverWait ua = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.3 Safari/537.36" cap = webdriver.DesiredCapabilities.PHANTOMJS cap["phantomjs.page.settings.resourceTimeout"] = 200000 cap["phantomjs.page.settings.loadImages"] = True cap["phantomjs.page.settings.disk-cache"] = True cap["phantomjs.page.settings.userAgent"] = ua cap["phantomjs.page.customHeaders.User-Agent"] =ua cap["phantomjs.page.customHeaders.Referer"] = "http://tj.ac.10086.cn/login/" driver = webdriver.PhantomJS(desired_capabilities=cap, service_args=['--ignore-ssl-errors=true'])