Loading... 老版selenium ``` import re, zipfile, requests, os from selenium import webdriver from selenium.webdriver.chrome.service import Service class driver(object): def __init__(self): self.root_path = os.path.abspath(os.path.dirname(__file__)).replace("test", "") # 获取项目根路径 self.service = Service(self.root_path + "\chromedriver.exe") self.options = webdriver.ChromeOptions() self.options.add_argument('--no-sandbox') # 解决DevToolsActivePort文件不存在的报错 self.options.add_argument('--disable-dev-shm-usage') self.options.add_argument('--disable-gpu') # 谷歌文档提到需要加上这个属性来规避bug self.options.add_argument('blink-settings=imagesEnabled=false') # 不加载图片, 提升速度 self.options.add_experimental_option("excludeSwitches", ['enable-automation', 'enable-logging']) self.options.add_argument('--headless') # 浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败 def unzip_file(self, src_file, dest_dir, *password): if password: password = password.encode() zf = zipfile.ZipFile(src_file) try: zf.extractall(path=dest_dir, pwd=password) except RuntimeError as e: print(e) zf.close() def go(self): i = 1 while (True): if i == 3: break try: driver = webdriver.Chrome(service=self.service, options=self.options) driver.implicitly_wait(6) break except Exception as msg: # 检测有无驱动 if "executable needs to be in PATH" in str(msg): downloadurl='http://chromedriver.storage.googleapis.com/100.0.4896.20/chromedriver_win32.zip' print('项目根目录未检测到驱动') print('正在下载默认驱动:'+downloadurl) response = requests.get(downloadurl) file_path = self.root_path + '\catch\\' if not os.path.exists(file_path): os.makedirs(file_path) file_name = 'chromedriver_100_0_4896_20.zip' with open(file_path + file_name, 'wb') as f: f.write(response.content) self.unzip_file(file_path + file_name, self.root_path) print("默认驱动:" + file_name + ' 下载安装成功') print('____________') continue # 检测驱动版本 if "version" in str(msg): reg = "Current browser version is.+with" chrome_version = re.search(reg, str(msg)).group().replace("Current browser version is ", "").replace( " with", "") file_path = self.root_path + '\catch\\' if not os.path.exists(file_path): os.makedirs(file_path) v = chrome_version.split('.') print("检测到项目根目录webdriver驱动不匹配,Chrome Version:" + chrome_version) v3 = requests.get('http://chromedriver.storage.googleapis.com/').text.split( f'<Key>{v[0]}.{v[1]}.{v[2]}.', 1)[1].split( '/chromedriver_linux64.zip', 1)[0] file_name = 'chromedriver_' + v[0] + '_' + v[1] + '_' + \ v[2] + '_' + v3 + '.zip' downloadurl = 'http://chromedriver.storage.googleapis.com/' + v[0] + '.' + \ v[1] + '.' + v[ 2] + '.' + v3 + '/chromedriver_win32.zip' print("更新中:" + downloadurl) response = requests.get(downloadurl) # 保存ChromeDriver到当前项目路径下的/catch目录下 with open(file_path + file_name, 'wb') as f: f.write(response.content) # 解压zip文件 self.unzip_file(file_path + file_name, self.root_path) print("更新完成!!!") print('------------') continue print(msg) print('____________') i += 1 return driver ``` --- 新版selenium无需单独下载驱动 ``` from selenium import webdriver def driver(): options = webdriver.ChromeOptions() options.add_argument('--no-sandbox') # 解决DevToolsActivePort文件不存在的报错 options.add_argument('--disable-dev-shm-usage') options.add_argument('--disable-gpu') # 谷歌文档提到需要加上这个属性来规避bug options.add_argument('blink-settings=imagesEnabled=false') # 不加载图片, 提升速度 options.add_experimental_option("excludeSwitches", ['enable-automation', 'enable-logging']) options.add_argument('--headless') # 浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败 driver = webdriver.Chrome() driver.implicitly_wait(6) return driver ``` 最后修改:2022 年 11 月 18 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果觉得我的文章对你有用,请随意赞赏
3 条评论
同城空降美女少妇a0gy.cn
做了几十年的项目 我总结了最好的一个盘(纯干货)coinsrore.com
文章中的实用建议和操作指南,让读者受益匪浅,值得珍藏。