被chromedriver调用时,对于M35以下内核原来可以使用的命令行依然有效,但对于某些命令行参数,已经被chromium 加入到bad flags list。当调用这些命令行参数打开浏览器时,将会在页面上出现不安全提示: 您使用的是不受支持的命令行标记: --ignore-certificate-errors。稳定性和安全性会有所下降。
当做自动化测试时,对于该提示可能会影响页面元素判断,导致用例执行失败;则可以在调用时,添加“--test-type”参数来避免出现该提示。 options.add_argument('--test-type') 发现网上还是很多人不知道怎么直接加argument,lol:https://code.google.com/p/selenium/issues/detail?id=7412#makechanges)
举个demo如下:
import os
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from os import environ
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import _winreg
import time
key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, r"Software\XXBrowser")
path = _winreg.QueryValueEx(key, "Path")[0]
chromedriver_path=path+'\\chromedriver.exe'
options = Options()
if str(environ.get('LOCALAPPDATA'))!='None':
user_data_dir= str(environ.get('LOCALAPPDATA'))+'\\XXBrowser\\User Data\\'
elif str(environ.get('HOMEPATH'))!= 'None':
user_data_dir= str(environ.get('HOMEPATH'))+'\\Local Settings\Application Data\\XXBrowser\\User Data\\'
else:
user_data_dir='None'
options.add_argument('--user-data-dir=%s' % user_data_dir)
options.add_argument('--no-first-run')
options.add_argument('--test-type')
driver = webdriver.Chrome(chromedriver_path,chrome_options=options) # Optional argument, if not specified will search path.
driver.get('http://www.google.com/xhtml');
time.sleep(5) # Let the user actually see something!
search_box = driver.find_element_by_name('q')
search_box.send_keys('ChromeDriver')
search_box.submit()
search_box.send_keys(Keys.RETURN)
time.sleep(5) # Let the user actually see something!
driver.quit()
参考: https://code.google.com/p/chromium/issues/detail?id=376424#makechanges https://code.google.com/p/chromedriver/issues/detail?id=799