-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
125 lines (95 loc) · 3.52 KB
/
main.py
File metadata and controls
125 lines (95 loc) · 3.52 KB
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
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time, os
def visit_url(url):
"""
Navigates to a given url.
Args:
url (str): The url of the site to visit (e.g., "https://stackexchange.com/").
sleep_time (int): The time in seconds to sleep before moving on.
"""
print(f"Visiting {url}")
driver.get(url)
WebDriverWait(driver, 10).until(
lambda driver: driver.execute_script('return document.readyState') == 'complete'
)
def visit_site(name):
"""
Navigates to a given site.
Args:
name (str): The name of the site to visit (e.g., 'superuser').
"""
url = f"https://{name}.com/users/current"
visit_url(url)
accept_cookies()
# Visit meta site
if '.stackexchange' in name:
namearray = name.split('.')
basename = namearray[0]
url = f"https://{basename}.meta.stackexchange.com/users/current"
else:
url = f"https://meta.{name}.com/users/current"
visit_url(url)
accept_cookies()
def accept_cookies():
isPresent = len(driver.find_elements(By.XPATH, '//*[@id="onetrust-accept-btn-handler"]')) > 0
if isPresent:
cookie_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="onetrust-accept-btn-handler"]'))
)
print(f"Clicking on accept cookie button: {cookie_button.text}")
cookie_button.click()
def login_stackexchange():
# Logs into Stack Exchange using the provided credentials.
name = 'stackexchange'
url = f"https://{name}.com/users/login/"
visit_url(url)
accept_cookies()
wait = WebDriverWait(driver, 10)
email = wait.until(EC.element_to_be_clickable((By.ID, 'email')))
password = wait.until(EC.element_to_be_clickable((By.ID, 'password')))
# Enter your credentials
emailStr = os.getenv('EMAIL')
passwordStr = os.getenv('PASSWORD')
email.send_keys(emailStr)
password.send_keys(passwordStr)
password.send_keys(Keys.RETURN)
time.sleep(4) # Wait for login to complete
url = f"https://{name}.com/users/15915140/anston-sorensen"
visit_url(url)
driver.save_screenshot("stackexchange.png")
# Visit meta site
url = f"https://meta.{name}.com/users/current"
visit_url(url)
accept_cookies()
if __name__ == '__main__':
# Set up Chrome options
chrome_options = Options()
chrome_options.add_argument("--headless") # Run Chrome in headless mode
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--remote-debugging-port=9222")
# Path to ChromeDriver
service = Service("chromedriver-linux64/chromedriver") # Adjust the path as needed
# Initialize the WebDriver
driver = webdriver.Chrome(service=service, options=chrome_options)
try:
# Log in to Stack Exchange
login_stackexchange()
# Visit other sites
sites = os.getenv('SITES')
sitesf = sites.split(', ')
for site in sitesf:
visit_site(site)
except Exception as e:
print(f"An error occurred: {e}")
finally:
# Always close the browser
print("Finished! Closing...")
driver.close()
driver.quit()