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 Files\python
更多參數設置可參考: 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'])