-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathchromedriver.py
75 lines (61 loc) · 2.34 KB
/
chromedriver.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# extension for https://sites.google.com/a/chromium.org/chromedriver/
# Experimental, maybe change in the future
# Created by <hzsunshx> 2017-01-20
from __future__ import absolute_import
import atexit
from selenium import webdriver
import subprocess
from urllib.error import URLError
class ChromeDriver(object):
def __init__(self, d, port=9515):
self._d = d
self._port = port
def _launch_webdriver(self):
print("start chromedriver instance")
p = subprocess.Popen(['chromedriver', '--port=' + str(self._port)])
try:
p.wait(timeout=2.0)
return False
except subprocess.TimeoutExpired:
return True
def driver(self, package=None, attach=True, activity=None, process=None):
"""
Args:
- package(string): default current running app
- attach(bool): default true, Attach to an already-running app instead of launching the app with a clear data directory
- activity(string): Name of the Activity hosting the WebView.
- process(string): Process name of the Activity hosting the WebView (as given by ps).
If not given, the process name is assumed to be the same as androidPackage.
Returns:
selenium driver
"""
app = self._d.current_app()
capabilities = {
'chromeOptions': {
'androidDeviceSerial': self._d.serial,
'androidPackage': package or app["package"],
'androidUseRunningApp': attach,
'androidProcess': process or app["package"],
'androidActivity': activity or app["activity"],
}
}
try:
dr = webdriver.Remote('http://localhost:%d' % self._port, capabilities)
except URLError:
self._launch_webdriver()
dr = webdriver.Remote('http://localhost:%d' % self._port, capabilities)
# always quit driver when done
atexit.register(dr.quit)
return dr
def windows_kill(self):
subprocess.call(['taskkill', '/F', '/IM', 'chromedriver.exe', '/T'])
if __name__ == '__main__':
import atx
d = atx.connect()
driver = ChromeDriver(d).driver()
elem = driver.find_element_by_link_text(u"登录")
elem.click()
driver.quit()