-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.py
63 lines (49 loc) · 2.21 KB
/
index.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
# -*- coding: utf8 -*-
import json
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
class Web:
def __init__(self, options, path='./chromedriver/chromedriver', screen_shot_path='./'):
self._driver = webdriver.Chrome(path, chrome_options=options)
self.screen_shot_count = 0
self.screen_shot_path = screen_shot_path
def get_screen_shot(self, element_id=None):
if element_id is not None:
ActionChains(self._driver).move_to_element(self._driver.find_element_by_id(element_id)).perform()
self._driver.save_screenshot('{}/{}.png'.format(self.screen_shot_path, self.screen_shot_count))
else:
self._driver.save_screenshot('{}/{}.png'.format(self.screen_shot_path, self.screen_shot_count))
print('screenshot {} saved!'.format(self.screen_shot_count))
self.screen_shot_count += 1
return self
def save_page_source(self):
with open("{}.html".format(self._driver.title), 'w') as f:
f.write(self._driver.page_source)
return self
def get_page_source(self):
return self._driver.page_source
def find_element_by_id(self, id_):
return self._driver.find_element_by_id(id_)
def find_element_by_xpath(self, xpath):
return self._driver.find_element_by_xpath(xpath)
def print_page(self):
print(self._driver.page_source)
def go_to(self, url):
self._driver.get(url)
return self
def main():
options = Options()
options.binary_location = './chromedriver/headless-chromium-tencent'
options.add_argument("--headless")
options.add_argument('--no-sandbox')
options.add_argument('--single-process')
options.add_argument('--disable-dev-shm-usage')
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2})
web = Web(options)
return json.loads(web.go_to('https://postman-echo.com/get').find_element_by_xpath('//body').text)
def test():
return platform.platform()
def main_handler(event, context):
return main()