-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect.py
72 lines (60 loc) · 1.92 KB
/
connect.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
#!/usr/bin/env python3
from sys import argv
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import (
NoSuchElementException, WebDriverException)
from urllib.parse import urlparse
BUTTON_XPATH: str = '/html/body/div[2]/div/div[1]/div[2]/div[1]/section[4]/div/div/div/div/form/div[2]/button'
def log_min(*args):
if args and 'connect button' in args[0]:
print('.', end='')
else:
print()
print(*args)
def ignore(*args):
pass
def main(args):
if len(args) > 2:
print('invalid usage, try either "-h", "-q", "-v", or no arguments')
return
if '-v' in args:
log = print
elif '-q' in args:
log = ignore
else:
log = log_min
options = webdriver.FirefoxOptions()
options.headless = True
responded = False
browser = webdriver.Firefox(options=options)
while not responded:
try:
browser.get('http://detectportal.firefox.com/canonical.html')
responded = True
except WebDriverException as e:
log(e.msg)
if "Timeout" not in str(e.msg):
return
browser.close()
browser = webdriver.Firefox(options=options)
browser.set_page_load_timeout(15)
connect_button = None
while connect_button is None:
browser.implicitly_wait(1)
try:
if (hostname := urlparse(browser.current_url).hostname) == 'support.mozilla.org':
log('Already Connected')
return
log(
f'trying to find connect button on {hostname}')
connect_button = browser.find_element(By.XPATH, BUTTON_XPATH)
except NoSuchElementException:
pass
log('found connect button!')
connect_button.click()
browser.implicitly_wait(3)
browser.close()
log('Connected')
if __name__ == '__main__':
main(argv)