1,谷歌浏览器默认位置
2,ChromeDriverManager 下载的驱动位置 其中admin为机器的用户名
def installDriver(self):
"""判断是否需要下载driver"""
"""找到本机谷歌浏览器版本"""
"""C:\Program Files\Google\Chrome\Application\122.0.6261.112
找到名字为版本号的文件夹"""
dirPath=r'C:\Program Files\Google\Chrome\Application'
version='122.0.6261.112'
for item in os.listdir(dirPath):
if re.search(r'\d{3}.\d.\d{4}.\d{3}',item):
version=re.search(r'\d{3}.\d.\d{4}.\d{3}',item).group(0)
break
username = getpass.getuser() #获取本机
driver_path = f"""C:\\Users\\{username}\\.wdm\\drivers\\chromedriver\\win64\\{version}\\chromedriver-win32\\chromedriver.exe"""
if not os.path.exists(driver_path):
driver_path = ChromeDriverManager().install()
return driver_path
def createDriver(self):
driver_path=self.installDriver()
op = webdriver.ChromeOptions()
# op.add_argument('headless')
op.add_argument('disable-infobars')
op.add_argument('--ignore-certificate-errors')
op.add_argument('--allow-insecure-localhost')
op.add_argument('-ignore -ssl-errors')
op.add_experimental_option('excludeSwitches', ['enable-automation']) # 以键值对的形式加入参数
prefs = {"profile.default_content_settings.popups": 1}
op.add_experimental_option('prefs', prefs)
script = '''
Object.defineProperty(navigator, 'webdriver', {
get: () => undefined
})
'''
self.driver = webdriver.Chrome(service=Service(driver_path))
self.driver.maximize_window()
self.driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {"source": script})
self.driver.execute_script(script)
script = '''
Object.defineProperty(navigator, 'plugins', {
get: () => 2
})
'''
self.driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {"source": script})
self.driver.set_page_load_timeout(60)