-
Notifications
You must be signed in to change notification settings - Fork 6
/
webdriver.py
92 lines (74 loc) · 3.19 KB
/
webdriver.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
import os
import logging
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
TIMEOUT_THRESHOLD = 3
class WebDriver:
def __init__(self):
self.driver = webdriver.Chrome(
executable_path=self.__get_chromedriver_path(), options=self.__get_options())
# self.driver.maximize_window()
def __get_chromedriver_path(self):
current_dir_path = os.path.dirname(__file__)
chromedriver_path = os.path.join(current_dir_path, "chromedriver")
# for Windows users
if os.name == "nt":
chromedriver_path += ".exe"
return chromedriver_path
def __get_options(self):
options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
return options
def __find_element_by_element_selector(self, element_selector):
try:
return WebDriverWait(self.driver, TIMEOUT_THRESHOLD).until(
EC.visibility_of_element_located(element_selector)
)
except TimeoutException:
logging.warning(f"Element isn't located yet: {element_selector}")
def get_current_url(self):
return self.driver.current_url
def open_url(self, url):
self.driver.get(url)
def refresh(self):
self.driver.refresh()
def find_element_by_class(self, class_name):
return self.__find_element_by_element_selector((By.CLASS_NAME, class_name))
def find_element_by_css_selector(self, css_selector):
return self.__find_element_by_element_selector((By.CSS_SELECTOR, css_selector))
def find_element_by_name(self, element_name):
return self.__find_element_by_element_selector((By.NAME, element_name))
def find_element_by_x_path(self, element_x_path):
return self.__find_element_by_element_selector((By.XPATH, element_x_path))
def find_element_by_visible_text(self, element_text):
case_insensitive_element_x_path = \
f"//*[text()[contains(translate(., '{element_text.upper()}', '{element_text.lower()}'), '{element_text.lower()}')]]"
return self.find_element_by_x_path(case_insensitive_element_x_path)
def find_element_within_iframe(self, iframe_title, element_x_path):
iframe = self.find_element_by_x_path(
f'//iframe[@title="{iframe_title}"]')
self.driver.switch_to.frame(iframe)
element = self.find_element_by_x_path(element_x_path)
self.driver.switch_to.default_content
return element
def click_on_element(self, element):
try:
element.click()
return True
except:
logging.warning("Unable to click on the element")
return False
def fill_in_input_field(self, input_field, text):
try:
for character in text:
input_field.send_keys(character)
return True
except:
logging.warning(
f"Unable to fill in the input field: {input_field}")
return False
def quit(self):
self.driver.quit()