-
Notifications
You must be signed in to change notification settings - Fork 0
/
autourl.py
128 lines (115 loc) · 3.79 KB
/
autourl.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
__author__ = 'maxh'
'''
wrap selenium actions in autourl.execute function.
for examples:
t = autourl()
t.geturl('http://www.baidu.com')
t.execute('id','kw','sendkey','test')
t.execute('id','su','click')
A suggest way to use this autourl is to store all
the actions into a OrderedDict collection, and call
autourl.execute() one by one
'''
#coding = utf-8
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import selenium.common.exceptions as sce
import time
import sys
class autourl:
def __init__(self, count=5):
'''
count: the retry times if the element can't be
at that time
'''
self.browser = webdriver.Chrome()
self.__count = count
def geturl(self, url):
self.browser.get(url)
def find_element(self, by, value, action):
retry = 0
while retry < self.__count:
try:
if action == 'click_value':
return self.browser.find_elements(by, value)
else:
return self.browser.find_element(by, value)
except sce.NoSuchElementException:
time.sleep(1)
retry += 1
raise sce.NoSuchElementException
def execute(self, elem, value, action, keys=''):
if elem == 'id':
by = By.ID
elif elem == 'name':
by = By.NAME
elif elem == 'class_name':
by = By.CLASS_NAME
elif elem == 'xpath':
by = By.XPATH
elif elem == 'url':
self.geturl(keys)
return
else:
print('unknown element' + elem)
sys.exit(1)
try:
m = self.find_element(by, value, action)
except sce.NoSuchElementException:
print("can't find %s:%s" % (elem, value))
sys.exit(1)
if action == "click":
retry = 0
sucess = False
while retry < self.__count:
try:
m.click()
sucess = True
return sucess
except sce.ElementNotVisibleException:
print('catch ElementNotVisibleException')
time.sleep(1)
retry += 1
if not sucess:
print('catch ElementNotVisibleException')
sys.exit(1)
elif action == 'click_value':
retry = 0
sucess = False
while retry < self.__count:
try:
for e in m:
if e.get_attribute('value') in keys.split(','):
e.click()
sucess = True
return sucess
except sce.ElementNotVisibleException:
print('catch ElementNotVisibleException')
time.sleep(1)
retry += 1
if not sucess:
print('catch ElementNotVisibleException')
sys.exit(1)
elif action == 'sendkey':
try:
m.clear()
except sce.InvalidElementStateException:
pass
m.send_keys(keys)
elif action == 'text':
self.result = eval(m.text)
elif action == 'select':
try:
Select(m).select_by_visible_text(keys)
except sce.NoSuchElementException:
print('Could not locate element with visible text:'+keys)
sys.exit(2)
else:
print('unsupported action type ' + action)
sys.exit(1)
if __name__ == '__main__':
t = autourl()
t.geturl('http://www.baidu.com')
t.execute('id', 'kw', 'sendkey','小米')
t.execute('id', 'su', 'click')