-
Notifications
You must be signed in to change notification settings - Fork 0
/
Browser.py
132 lines (110 loc) · 6.11 KB
/
Browser.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import undetected_chromedriver as uc
import time
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from bs4 import BeautifulSoup
class Browser:
"""
A wrapper class for Selenium WebDriver to simplify interactions with web pages.
This class provides simplified methods for common web automation tasks such as
navigating to a URL, clicking buttons, selecting from dropdowns, and parsing
the page source with BeautifulSoup.
Attributes:
_browser: A Selenium WebDriver instance for browser automation.
Methods:
__init__: Initializes the Browser with an undetected ChromeDriver instance.
set_value_of_select_element: Selects an option in a dropdown by its value.
navigate: Navigates the browser to a specified URL.
wait_and_click_button: Waits for a button to be clickable and then clicks it.
get_single_element: Returns a single web element found by a specific selector.
get_array_of_elements: Returns a list of web elements found by a specific selector.
click_button: Clicks a button found by a specific selector.
execute: Executes a JavaScript script in the context of the current page.
scroll_to_bottom_of_page: Scrolls the browser window to the bottom of the page.
wait_for_element_to_load: Waits for an element to be present on the page.
parse_page_source: Parses the current page source with BeautifulSoup.
clear_input_element: Clears the text of an input field.
_write_to_input_element: Writes text to an input field (private method).
write_input: Writes text to an input field.
submit_input: Writes text to an input field and submits the form.
upload_file: Uploads a file to an input field of type 'file'.
quit: Closes the browser and quits the WebDriver session.
"""
def __init__(self):
# Initialize the Browser object with an undetected ChromeDriver instance.
self._browser = uc.Chrome(use_subprocess=False)
def set_value_of_select_element(self, find_element_by, selector_value,
input_value):
# Selects an option in a dropdown by its value.
Select(self._browser.find_element(
find_element_by, selector_value)).select_by_value(input_value)
def navigate(self, url):
# Navigates the browser to a specified URL.
self._browser.get(url)
def wait_and_click_button(self, find_element_by, selector_value):
# Waits for a button to be clickable and then clicks it.
button = WebDriverWait(driver=self._browser,
timeout=10,
poll_frequency=1).until(
EC.element_to_be_clickable(
(find_element_by, selector_value)))
if button:
button.click()
return True
return False
def get_single_element(self, find_element_by, selector_value):
# Returns a single web element found by a specific selector.
return self._browser.find_element(find_element_by, selector_value)
def get_array_of_elements(self, find_element_by, selector_value):
# Returns a list of web elements found by a specific selector.
return self._browser.find_elements(find_element_by, selector_value)
def click_button(self, find_element_by, selector_value):
# Clicks a button found by a specific selector.
self._browser.find_element(find_element_by, selector_value).click()
def execute(self, script, args=None):
# Executes a JavaScript script in the context of the current page.
if args is None:
self._browser.execute_script(script)
else:
self._browser.execute_script(script, args)
def scroll_to_bottom_of_page(self):
# Scrolls the browser window to the bottom of the page.
self.execute('window.scrollTo(0, document.body.scrollHeight);')
def wait_for_element_to_load(self, find_element_by, selector_value):
# Waits for an element to be present on the page.
WebDriverWait(self._browser, 10).until(
EC.presence_of_element_located((find_element_by, selector_value)))
def parse_page_source(self, features='lxml'):
# Parses the current page source with BeautifulSoup.
return BeautifulSoup(self._browser.page_source, features=features)
def clear_input_element(self, find_input_field_by, input_field_name):
# Clears the text of an input field.
self._browser.find_element(find_input_field_by,
input_field_name).clear()
def _write_to_input_element(self, find_input_field_by, input_field_name,
input_value):
# Writes text to an input field (private method).
search_field = self._browser.find_element(find_input_field_by,
input_field_name)
search_field.clear()
time.sleep(1)
search_field.send_keys(input_value)
return search_field
def write_input(self, find_input_field_by, input_field_name, input_value):
# Writes text to an input field.
self._write_to_input_element(find_input_field_by, input_field_name,
input_value)
def submit_input(self, find_input_field_by, input_field_name, input_value):
# Writes text to an input field and submits the form.
search_field = self._write_to_input_element(find_input_field_by,
input_field_name,
input_value)
search_field.submit()
def upload_file(self, find_input_field_by, input_field_name, file_path):
# Uploads a file to an input field of type 'file'.
self._browser.find_element(find_input_field_by,
input_field_name).send_keys(file_path)
def quit(self):
# Closes the browser and quits the WebDriver session.
self._browser.quit()