from selenium.common.exceptions import StaleElementReferenceException, TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import requests

default_timeout = 10

class count_zero_or_invisible(object):

    def __init__(self, locator):
        self.locator = locator

    def __call__(self, driver):
        try:
            elems = driver.find_elements(*self.locator)
            if len(elems) == 0:
                return True
            else:
                for elem in elems:
                    if elem.is_displayed():
                        return False
                return True
        except StaleElementReferenceException:
            return False


class count_non_zero_and_visible(object):

    def __init__(self, locator):
        self.locator = locator

    def __call__(self, driver):
        try:
            elems = driver.find_elements(*self.locator)
            if not elems or len(elems) == 0:
                return False
            else:
                for elem in elems:
                    if elem.is_displayed():
                        return elems
                return False
        except StaleElementReferenceException:
            return False


class count_non_zero_and_clickable(object):

    def __init__(self, locator):
        self.locator = locator

    def __call__(self, driver):
        try:
            elems = driver.find_elements(*self.locator)
            if not elems or len(elems) == 0:
                return False
            else:
                for elem in elems:
                    if elem.is_displayed() and elem.is_enabled():
                        return elems
                return False
        except StaleElementReferenceException:
            return False



def goto(url):
    global driver