Skip to content

Instantly share code, notes, and snippets.

@umrashrf
Created December 5, 2018 08:33
Show Gist options
  • Save umrashrf/ac629814794285fbdeeb6d6fe2289b45 to your computer and use it in GitHub Desktop.
Save umrashrf/ac629814794285fbdeeb6d6fe2289b45 to your computer and use it in GitHub Desktop.
"""
Generalized browser module for selenium web driver
"""
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from . import settings
from .selenium.web_element import WebElement
class Browser(webdriver.Firefox):
"""
Generalized Browser class
Defaults: webdriver.Firefox
"""
_web_element_cls = WebElement
def __init__(self, *args, **kwargs):
self.timeout = settings.SELENIUM_ELEMENT_TIMEOUT
super().__init__(*args, **kwargs)
def css(self, *args, **kwargs):
"""
Use css library to enable css based selectors
"""
raise NotImplementedError
def id(self, id_): # pylint: disable=
"""
Handy function to find element by id
Built-in support for waiting for element to be visible
"""
WebDriverWait(self, self.timeout).until(
EC.presence_of_element_located((By.ID, id_))
)
return self.find_element_by_id(id_)
def by_name(self, name):
"""
Handy function to find element by name
Built-in support for waiting for element to be visible
"""
WebDriverWait(self, self.timeout).until(
EC.presence_of_element_located((By.NAME, name))
)
return self.find_element_by_name(name)
def tag_name(self, name):
"""
Handy function to find element by tag name
Built-in support for waiting for element to be visible
"""
WebDriverWait(self, self.timeout).until(
EC.presence_of_element_located((By.TAG_NAME, name))
)
return self.find_element_by_tag_name(name)
def xpath(self, xpath):
"""
Handy function to find element by xpath
Built-in support for waiting for element to be visible
"""
WebDriverWait(self, self.timeout).until(
EC.presence_of_element_located((By.XPATH, xpath))
)
return self.find_element_by_xpath(xpath)
def xpath_all(self, xpath):
"""
Handy function to find elements by xpath
Built-in support for waiting for element to be visible
"""
WebDriverWait(self, self.timeout).until(
EC.presence_of_element_located((By.XPATH, xpath))
)
return self.find_elements_by_xpath(xpath)
"""
Custom WebElement to add additional support
"""
import time
import selenium.webdriver.remote.webelement
class WebElement(selenium.webdriver.remote.webelement.WebElement):
"""
Custom WebElement class for additional support
"""
def send_value(self, value, clear=True, human=False):
"""
Clears the input field before sending the value
Support human like natural writing to the input element
"""
if clear:
self.clear()
if human:
for v in value: # pylint: disable=C0103
self.send_keys(v)
time.sleep(0.250)
else:
self.send_keys(value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment