-
Notifications
You must be signed in to change notification settings - Fork 0
/
easySelenium.py
171 lines (167 loc) · 6.41 KB
/
easySelenium.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/python3
import sys
import os
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
class easySelenium:
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
chrome_data = os.path.join(sys.path[0], "chrome_data")
if sys.platform ==('linux1' or'linux2'):
driver_location ="/usr/bin/chromedriver" or os.path.join(sys.path[0], "chromedriver")
chrome_location ="/usr/bin/google-chrome"
options.binary_location = chrome_location
elif sys.platform == 'win32':
driver_location =os.path.join(sys.path[0], "chromedriver.exe")
options.add_argument("user-data-dir="+str(chrome_data))
browser=any
firstTabSet:bool = False
isBrowserOff:bool = False
def __init__(self,headerless:bool=False) -> None:
self.options.headless = headerless
self.browser = webdriver.Chrome(executable_path=self.driver_location,options=self.options)
def isExist(self,xpath:str ="",by='By.XPATH'):
'''
'by' parameter can satisfied with:
[
By.XPATH -------> DEFAULT
By.NAME
By.LINK_TEXT
By.CLASS_NAME
By.CSS_SELECTOR
By.PARTIAL_LINK_TEXT
By.ID
By.TAG_NAME
]
'''
try:
match by:
case 'By.XPATH':
self.browser.find_element(By.XPATH,xpath)
case 'By.NAME':
self.browser.find_element(By.NAME,xpath)
case 'By.LINK_TEXT':
self.browser.find_element(By.LINK_TEXT,xpath)
case 'By.CLASS_NAME':
self.browser.find_element(By.CLASS_NAME,xpath)
case 'By.CSS_SELECTOR':
self.browser.find_element(By.CSS_SELECTOR,xpath)
case 'By.PARTIAL_LINK_TEXT':
self.browser.find_element(By.PARTIAL_LINK_TEXT,xpath)
case 'By.ID':
self.browser.find_element(By.ID,xpath)
case 'By.TAG_NAME':
self.browser.find_element(By.TAG_NAME,xpath)
return True
except:
return False
def waitUntillExist(self,xpath:str ="",by='By.XPATH',timeout:int=600,inbetweenSleep:int = 0):
clock:int=0
while True:
if self.isExist(xpath=xpath,by=by):
return True
else:
##Rough clock for timeout//could be done better
time.sleep(1)
clock += 1
if clock >= timeout:
return False
##
time.sleep(inbetweenSleep)
def waitForUrl(self,url:str ="",by='By.XPATH',timeout:int=600,inbetweenSleep:int = 0):
clock:int=0
while True:
if str(self.browser.current_url) == str(url):
return True
else:
##Rough clock for timeout//could be done better
time.sleep(1)
clock += 1
if clock >= timeout:
return False
##
time.sleep(inbetweenSleep)
def waitForUrlChange(self,url:str ="",by='By.XPATH',timeout:int=600,inbetweenSleep:int = 0):
clock:int=0
while True:
if not(str(self.browser.current_url) == str(url)):
return True
else:
##Rough clock for timeout//could be done better
time.sleep(1)
clock += 1
if clock >= timeout:
return False
##
time.sleep(inbetweenSleep)
def switchTab(self,tab:int=0) -> None:
self.browser.switch_to.window(self.browser.window_handles[tab])
def scroll(self,height=0,timeout:int=120,pageloadSleep:int=5) -> None:
pageHeight = self.browser.execute_script("return document.documentElement.scrollHeight")
previousHeight = 0
clock:int = 0
while True:
if previousHeight == pageHeight or (height>0 and height == pageHeight):
break
previousHeight = pageHeight
self.browser.execute_script("window.scrollTo(0, " + str(pageHeight) + ");")
time.sleep(pageloadSleep)
pageHeight = self.browser.execute_script("return document.documentElement.scrollHeight")
##Another Rough clock for timeout//could be done better
clock += pageloadSleep
if clock >= timeout:
break ##
def isInternetON(self):
import urllib.request
try:
urllib.request.urlopen('http://google.com')
return True
except:
return False
def waitForInternet(self,timeout:int=600,inbetweenSleep:int = 0):
clock:int=0
while True:
if self.isInternetON():
return True
else:
##Rough clock for timeout//could be done better
time.sleep(1)
clock += 1
if clock >= timeout:
return False
##
time.sleep(inbetweenSleep)
def back(self):
currenturl = self.browser.current_url
self.browser.back()
self.waitForUrlChange(currenturl)
def changeUrl(self,url:str=''):
self.firstTabSet = False
self.open(url)
self.waitForUrlChange(url)
def free(self) -> None:
self.browser.quit()
self.isBrowserOff = True
def open(self,url:str) -> None:
##ensure https:// is in url
strpos:int = 0
for i in url:
if i == 'https://'[strpos]:
strpos += 1
if strpos == 0:
url = 'https://' + url
break
elif strpos >= 7:
break
##
if self.isBrowserOff:#Reopening browser once quit
self.__init__(self.options.headless)
self.firstTabSet = False
self.isBrowserOff:bool = False
if self.firstTabSet:
self.browser.execute_script("window.open('"+ url +"','_blank');")
self.switchTab(len(self.browser.window_handles)-1)
self.browser.get(url)
self.firstTabSet = True