-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselenium_helpers.py
36 lines (29 loc) · 1.38 KB
/
selenium_helpers.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
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.common.exceptions import NoSuchElementException, StaleElementReferenceException
from selenium.webdriver.remote.webdriver import WebDriver
TIMEOUT = 10 # in seconds
def get_chrome_options() -> Options:
opts = Options()
opts.add_argument("--headless")
opts.add_argument("--incognito")
opts.add_argument("--no-sandbox")
opts.add_argument("--disable-dev-shm-usage")
opts.add_argument("--window-size=800,600")
opts.add_argument("--ignore-certificate-errors")
chrome_prefs = {}
opts.experimental_options["prefs"] = chrome_prefs
chrome_prefs["profile.default_content_settings"] = {"images": 2}
return opts
def get_by_xpath_with_wait(browser: WebDriver, xpath):
ignored_exceptions = (NoSuchElementException, StaleElementReferenceException)
return WebDriverWait(browser, TIMEOUT, ignored_exceptions=ignored_exceptions).until(
EC.presence_of_element_located((By.XPATH, xpath)))
def wait_for_xpath_to_be_stale(browser: WebDriver, xpath):
try:
elem = browser.find_element_by_xpath(xpath)
WebDriverWait(browser, TIMEOUT).until(EC.staleness_of(elem))
except NoSuchElementException:
pass