forked from toxtli/twitter-accounts-creator-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SeleniumHelper.py
154 lines (127 loc) · 3.93 KB
/
SeleniumHelper.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
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
class SeleniumHelper:
WAIT = 99999
driver = None
def loadPage(self, page):
try:
self.driver.get(page)
return True
except:
return False
def submitForm(self, element):
try:
element.submit()
return True
except TimeoutException:
return False
def submitFormSelector(self, selector):
try:
element = self.getElement(selector)
element.submit()
return True
except TimeoutException:
return False
def waitShowElement(self, selector, wait=99999):
try:
wait = WebDriverWait(self.driver, wait)
element = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, selector)))
return element
except:
return None
def waitHideElement(self, selector, wait):
try:
wait = WebDriverWait(self.driver, wait)
element = wait.until(EC.invisibility_of_element_located((By.CSS_SELECTOR, selector)))
return element
except:
return None
def getElementFrom(self, fromObject, selector):
try:
return fromObject.find_element_by_css_selector(selector)
except NoSuchElementException:
return None
def getElementsFrom(self, fromObject, selector):
try:
return fromObject.find_elements_by_css_selector(selector)
except NoSuchElementException:
return None
def getElement(self, selector):
return self.getElementFrom(self.driver, selector)
def getElements(self, selector):
return self.getElementsFrom(self.driver, selector)
def getElementFromValue(self, fromObject, selector):
element = self.getElementFrom(fromObject, selector)
if element:
return element.text
return None
def getElementValue(self, selector):
element = self.getElement(selector)
if element:
return element.text
return None
def getElementFromAttribute(self, fromObject, selector, attribute):
element = self.getElementFrom(fromObject, selector)
if element:
return element.get_attribute(attribute)
return None
def getElementAttribute(self, selector, attribute):
element = self.getElement(selector)
if element:
return element.get_attribute(attribute)
return None
def getParentNode(self, node):
return node.find_element_by_xpath('..')
def getChildNodes(self, node):
return node.find_elements_by_xpath('./*')
def selectAndWrite(self, field, value):
fieldObject = self.getElement(field)
fieldObject.send_keys(value)
return fieldObject
def waitAndWrite(self, field, value):
fieldObject = self.waitShowElement(field, self.WAIT)
fieldObject.send_keys(value)
return fieldObject
def waitAndClick(self, field):
fieldObject = self.waitShowElement(field, self.WAIT)
self.click(fieldObject)
return fieldObject
def clickSelector(self, selector):
element = self.getElement(selector)
actions = webdriver.ActionChains(self.driver)
actions.move_to_element(element)
actions.click(element)
actions.perform()
def click(self, element):
actions = webdriver.ActionChains(self.driver)
actions.move_to_element(element)
actions.click(element)
actions.perform()
def moveToElement(self, element):
self.driver.execute_script("return arguments[0].scrollIntoView();", element)
actions = webdriver.ActionChains(self.driver)
actions.move_to_element(element)
actions.perform()
def saveScreenshot(self, path="screenshot.png"):
self.driver.save_screenshot(path)
def getBetween(self, text, strInit, strEnd):
exit = None
arr1 = text.split(strInit)
if len(arr1) > 1:
arr2 = arr1[1].split(strEnd)
exit = arr2[0]
return exit
def getCharsFrom(self, text, strInit, count):
exit = None
arr1 = text.split(strInit)
if len(arr1) > 1:
substr = arr1[1]
exit = substr[:count]
return exit
def close(self):
self.driver.quit()