forked from kuro7766/Docker-Selenium-Firefox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
selelib.py
108 lines (90 loc) · 3.29 KB
/
selelib.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import os
import pickle
import time
import re
import ml
from selenium.common.exceptions import NoSuchElementException
from sys import platform
def platform_kill(driver=None):
print('platform::', str(platform))
if platform == "linux" or platform == "linux2":
# linux
driver.close()
driver.quit()
os.system('''ps aux|grep firefox|awk '{print $2}' |xargs kill -9''')
print('killing all firefox processes...')
elif platform == "win32" or platform == "win64":
# Windows...
print('win32 killing ...')
# print(driver)
if driver:
driver.close()
driver.quit()
def screen_shot(driver, dir_pic='默认截图.png'):
# ml.ensure_dir('截图')
# driver.save_screenshot('截图/' + str(datetime.datetime.now()) + '.png')
driver.save_screenshot(dir_pic)
# def all_finish():
# if platform == "linux" or platform == "linux2":
# # linux
# os.system('''ps aux|grep firefox|awk '{print $2}' |xargs kill -9''')
# os.system('''find /tmp -name "rust*"|xargs rm -rf''')
# print('killing and cleaning caches ...')
# def load_cookie(cookie_path):
# if os.path.exists(cookie_path):
# with open(cookie_path, "rb") as f:
# cookies = pickle.load(f)
# print('dump_cookie ::: ', cookies)
# f.close()
# return cookies
class lib:
def __init__(self, driver):
self.driver = driver
def findall(self, pattern):
text = self.get_html()
return re.findall(pattern, text)
def wait_until_text(self, pattern):
while not self.findall(pattern):
time.sleep(1)
def get_html(self):
return self.driver.execute_script("return document.getElementsByTagName('html')[0].innerHTML")
def wait_until_xpath(self, xpath):
find = False
while not find:
try:
if self.driver.find_element_by_xpath(xpath):
find = True
return self.driver.find_element_by_xpath(xpath)
else:
time.sleep(1)
except NoSuchElementException as e:
print(e)
return self.driver.find_element_by_xpath(xpath)
def load_cookie(self, cookie_path, load_in=True):
if os.path.exists(cookie_path):
with open(cookie_path, "rb") as f:
cookies = pickle.load(f)
print('dump_cookie ::: ', cookies)
if load_in:
for cookie in cookies:
try:
self.driver.add_cookie(cookie)
except Exception:
pass
f.close()
return cookies
def dump_cookie(self, cookie_path):
with open(cookie_path, "wb") as f:
pickle.dump(self.driver.get_cookies(), f)
print('dump_cookie ::: ', self.driver.get_cookies())
f.close()
return self.driver.get_cookies()
def screen_shot(self, path):
self.driver.save_screenshot(path)
return path
def get_current_window_text_ocr(self):
oo = ml.ocr(self.screen_shot('tmp.png'))
s = []
for i in oo['words_result']:
s.append(i['words'])
return ''.join(s)