-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselenium-template.py
executable file
·65 lines (56 loc) · 2.46 KB
/
selenium-template.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
'''
[FILENAME]
[AUTHOR]
Created: [DATE]
Updated: [DATE]
Description: [DESCRIPTION]
'''
# This part is used for https://github.com/hussein-esmail7/template-maker
# templateDescription: Python Selenium Web Scraper
import os
import sys # To exit the program
from selenium import webdriver
from selenium.common.exceptions import *
from webdriver_manager.chrome import ChromeDriverManager
# from selenium.webdriver.support.ui import Select # Used to select from drop down menus
from selenium.webdriver.chrome.service import Service # Used to set Chrome location
from selenium.webdriver.chrome.options import Options # Used to add aditional settings (ex. run in background)
from selenium.webdriver.common.by import By # Used to determine type to search for (normally By.XPATH)
# from selenium.webdriver.common.keys import Keys # Used for pressing special keys, like 'enter'
# ========= VARIABLES ===========
bool_run_in_background = True
target_site = "https://google.com"
# ========= COLOR CODES =========
color_end = '\033[0m'
color_darkgrey = '\033[90m'
color_red = '\033[91m'
color_green = '\033[92m'
color_yellow = '\033[93m'
color_blue = '\033[94m'
color_pink = '\033[95m'
color_cyan = '\033[96m'
color_white = '\033[97m'
color_grey = '\033[98m'
# ========= COLORED STRINGS =========
str_prefix_q = f"[{color_pink}Q{color_end}]\t "
str_prefix_y_n = f"[{color_pink}y/n{color_end}]"
str_prefix_err = f"[{color_red}ERROR{color_end}]\t "
str_prefix_done = f"[{color_green}DONE{color_end}]\t "
str_prefix_info = f"[{color_cyan}INFO{color_end}]\t "
def main():
options = Options()
if bool_run_in_background:
options.add_argument("--headless") # Adds the argument that hides the window
service = Service(ChromeDriverManager(log_level=0).install())
driver = webdriver.Chrome(service=service, options=options)
driver.set_window_size(200, 1000) # Window size
driver.get(target_site)
# ADDITIONAL CODE HERE
# Example of getting elements by XPATH
# time_unformatted = driver.find_element(By.XPATH, '//time/span[1]').text + " /" + driver. find_element(By.XPATH, '//time/span[2]').text¬
# Cleanup
driver.close() # Close the browser
options.extensions.clear() # Clear the options that were set
sys.exit() # Exit the program
if __name__ == "__main__":
main()