-
Notifications
You must be signed in to change notification settings - Fork 18
/
stack_overflow_page.py
62 lines (41 loc) · 1.88 KB
/
stack_overflow_page.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
import os
from dotenv import load_dotenv
import logging
import logging.config
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
from sendgrid_helper import send_mail
load_dotenv()
logging.config.fileConfig('logging.conf')
def login():
logging.info("Logging into stackoverflow.com")
email = os.environ.get('STACK_OVERFLOW_EMAIL')
password = os.environ.get('STACK_OVERFLOW_PASSWORD')
display_name = os.environ.get('STACK_OVERFLOW_DISPLAY_NAME')
if None in (email, password, display_name):
logging.error("Set 'STACK_OVERFLOW_EMAIL' 'STACK_OVERFLOW_PASSWORD' 'STACK_OVERFLOW_DISPLAY_NAME' env "
"variables to successfully log into Stack Overflow for "+email+", "+display_name)
return
driver = webdriver.Chrome(ChromeDriverManager().install())
try:
driver.get("https://stackoverflow.com")
driver.find_element(By.LINK_TEXT, "Log in").click()
driver.find_element(By.ID, "email").send_keys(email)
driver.find_element(By.ID, "password").send_keys(password)
driver.find_element(By.ID, "submit-button").submit()
driver.find_element(By.PARTIAL_LINK_TEXT, display_name).click()
elem = WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.ID, "js-daily-access-calendar-container"))
)
logging.info("Logged into stackoverflow.com and accessed profile page - " + elem.text)
except Exception as e:
message = "An error occurred while trying to access stackoverflow.com!"
logging.error(message, e)
send_mail("Error at login!", message + str(e))
finally:
driver.close()
if __name__ == "__main__":
login()