forked from t0mer/HAvid-19
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
105 lines (85 loc) · 3.47 KB
/
helpers.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
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from datetime import date
from datetime import time
from datetime import datetime
import time
import os
from os import path
from selenium.common.exceptions import InvalidSessionIdException
from loguru import logger
#### Setting ChromeOptions ####
def GetBrowser():
options = webdriver.ChromeOptions()
options.add_argument("--incognito")
options.add_argument("--headless")
options.add_argument("disable-gpu")
options.add_argument("--no-sandbox")
options.add_argument('--start-maximized')
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--window-size=360,640")
options.add_argument('--ignore-certificate-errors')
browser = webdriver.Chrome(executable_path='/opt/chromedriver-86.0.4240.22/chromedriver', options=options)
return browser
def GetMobileBrowser():
options = webdriver.ChromeOptions()
options.add_argument("--incognito")
options.add_argument("--headless")
options.add_argument("disable-gpu")
options.add_argument("--no-sandbox")
options.add_argument('--start-maximized')
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--window-size=360,640")
options.add_argument('--ignore-certificate-errors')
options.add_argument('--user-agent=Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.75 Mobile/14E5239e Safari/602.1')
browser = webdriver.Chrome(executable_path='/opt/chromedriver-86.0.4240.22/chromedriver', options=options)
return browser
##### Screenshot for mobile view - like webtop #####
def mobile_screenshot(browser,Image):
if path.exists(Image):
os.remove(Image)
logger.info(browser)
browser.set_window_size(380, 660) #the trick
time.sleep(2)
browser.save_screenshot(Image)
browser.close()
#### Screenshot for regular view ####
def fullpage_screenshot(browser,Image):
if path.exists(Image):
os.remove(Image)
logger.info(browser)
browser.set_window_size(800, 600) #the trick
time.sleep(2)
browser.save_screenshot(Image)
#### Screenshot for regular view ####
def largepage_screenshot(browser,Image):
if path.exists(Image):
os.remove(Image)
logger.info(browser)
browser.set_window_size(800, 2070) #the trick
time.sleep(2)
browser.save_screenshot(Image)
#### Browser state logging ####
def log_browser(browser):
logger.debug(f"Opened page. Url: {browser.current_url}, size: {len(browser.page_source)}")
def ping(browser, page):
try:
browser.get('https://bots.techblog.co.il/' + page + '.html')
except:
logger.info("Unable to ping")
def get_clear_browsing_button(browser):
"""Find the "CLEAR BROWSING BUTTON" on the Chrome settings page."""
return browser.find_element_by_css_selector('* /deep/ #clearBrowsingDataConfirm')
def clear_cache(browser, timeout=60):
"""Clear the cookies and cache for the ChromeDriver instance."""
# navigate to the settings page
browser.get('chrome://settings/clearBrowserData')
# wait for the button to appear
wait = WebDriverWait(browser, timeout)
wait.until(get_clear_browsing_button)
# click the button to clear the cache
get_clear_browsing_button(browser).click()
# wait for the button to be gone before returning
wait.until_not(get_clear_browsing_button)