-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprevious_version.py
More file actions
73 lines (63 loc) · 2.81 KB
/
previous_version.py
File metadata and controls
73 lines (63 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# いろんなものをインポート
import re
import zipfile
from pip._vendor import requests
from selenium import webdriver
from selenium.common.exceptions import SessionNotCreatedException,WebDriverException
from bs4 import BeautifulSoup
import urllib.request
# 必要な情報
#実行環境上のChromeブラウザのバージョンの箱
current_version = ""
#ウェブドライバーページ
webdriver_url = "https://chromedriver.storage.googleapis.com/"
#Windows用のファイル名
file_name = "chromedriver_win32.zip"
try:
driver = webdriver.Chrome('chromedriver')
print('起動に成功。問題なし。')
except(FileNotFoundError,WebDriverException,SessionNotCreatedException) as e:
if type(e) == SessionNotCreatedException:
print("WebDriverが古いです。")
tmp = re.split("\n",str(e))
tmp = re.split(" ",tmp[1])
tmp = re.split("\.",tmp[4])
current_version = tmp[0]+"."+tmp[1]+"."+tmp[2]
print("chromeのversionは "+current_version)
elif type(e)==FileNotFoundError or type(e)== WebDriverException:
print("WebDriverが見つかりません。")
else:
print("不明な例外です。")
print(e)
exit()
print('エラーが出たので最新版のWebDriverをダウンロード開始')
response = requests.get(webdriver_url)
soup = BeautifulSoup(response.text,"lxml-xml")
for version in reversed(soup.find_all("Key")):
if((current_version != "" and version.text.startswith(current_version))
or (current_version == "" and version.text.endswith(file_name))):
get_version = re.sub("/.*","",version.text)
file_url = webdriver_url + get_version + "/" + file_name
save_path = "./download_webdriver.zip"
print(get_version+' のバージョンをダウンロードします。')
with urllib.request.urlopen(file_url) as download_file:
data = download_file.read()
with open(save_path, mode='wb') as save_file:
save_file.write(data)
import zipfile
with zipfile.ZipFile("./download_webdriver.zip") as obj_zip:
obj_zip.extractall("./")
try:
driver = webdriver.Chrome('chromedriver')
print('起動に成功。問題なし。')
break
except(SessionNotCreatedException) as e:
if type(e) == SessionNotCreatedException:
print('失敗。違うバージョンを試します。')
print("WebDriverが古いです。")
tmp = re.split("\n",str(e))
tmp = re.split(" ",tmp[1])
tmp = re.split("\.",tmp[4])
current_version = tmp[0]+"."+tmp[1]+"."+tmp[2]
driver.quit()
exit()