-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathhelper_funcs.py
70 lines (60 loc) · 2.85 KB
/
helper_funcs.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
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
import logging
logging.basicConfig(format='%(asctime)s - %(levelname)s: %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
# logging.basicConfig(format='%(asctime)s - %(levelname)s: %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p',
# level=logging.INFO) # For logging to console, Debugging purposes.
class HelperFn:
def __init__(self, driver):
self.driver = driver
def wait_for_element(self, xpath, timeout=22):
try:
WebDriverWait(self.driver, timeout).until(EC.presence_of_element_located((By.XPATH, xpath)))
logging.info("# Element '%s' is present." % xpath)
except TimeoutException:
logging.error("# Element '%s' is not present." % xpath)
def is_element_present(self, xpath):
try:
self.driver.find_element(By.XPATH, xpath)
logging.info("# Element '%s' is present." % xpath)
except NoSuchElementException:
logging.error("# Element '%s' is not present." % xpath)
return False
return True
def wait_for_element_visible(self, xpath, timeout=22):
try:
WebDriverWait(self.driver, timeout).until(EC.visibility_of_element_located((By.XPATH, xpath)))
logging.info("# Element '%s' is visible." % xpath)
except TimeoutException:
logging.error("# Element '%s' is not visible." % xpath)
def is_element_visible(self, xpath):
try:
logging.info("# Element '%s' is visible." % xpath)
return self.driver.find_element(By.XPATH, xpath).is_displayed()
except NoSuchElementException:
logging.error("# Element '%s' is not visible." % xpath)
return False
def find_element(self, xpath):
try:
element = self.driver.find_element(By.XPATH, xpath)
logging.info("# Element '%s' is found." % xpath)
except NoSuchElementException:
logging.error("# Element '%s' is not found." % xpath)
return False
return element
def find_elements(self, xpath):
try:
elements = self.driver.find_elements(By.XPATH, xpath)
logging.info("# Element '%s' is found." % xpath)
except NoSuchElementException:
logging.error("# Element '%s' is not found." % xpath)
return False
return elements
def wait_for_x_seconds(self, seconds):
logging.info("# Waiting for %s seconds." % seconds)
self.driver.implicitly_wait(seconds)
logging.info("# Done waiting for %s seconds." % seconds)