from bfs.bfs10063.bfs10255 import bfs10256
from decorator import decorator
from selenium.common.exceptions import StaleElementReferenceException, NoSuchFrameException
from selenium.webdriver.common.action_chains import ActionChains
from urllib2 import URLError
import sys
class bfs10415(object):
  def __init__(self, target):
    self.target = target

  def __getattr__(self, item):
    return getattr(self.target, item)

  def unwrap(self):
    return self.target

  def __hash__(self):
    return hash(self.target)

  def __eq__(self, bfs10414):
    return (self.target == bfs10414.target)

  def __ne__(self, bfs10416):
    return (not (self == bfs10416))


class bfs10420(bfs10415):
  def __init__(self, target):
    super(bfs10420, self).__init__(target)
    self.bfs10417 = None

  def bfs10421(self):
    return ActionChains(self.target)

  def bfs10422(self, web_element):
    if (not self.bfs10417):
      return 0

    if hasattr(self.bfs10417, 'location'):
      try:
        bfs10423 = self.bfs10417.location
      except StaleElementReferenceException:
        return 0
      else:
        return bfs10423.bfs10351(web_element.location)

    else:
      return 0


  def find_elements_by_name(self, name):
    return (self.target.find_elements_by_name(name) or [])

  def find_elements_by_xpath(self, bfs10424):
    return (self.target.find_elements_by_xpath(bfs10424) or [])

  def find_elements_by_css_selector(self, bfs10425):
    return (self.target.find_elements_by_css_selector(bfs10425) or [])

  def bfs10427(self):
    return (self.browser_name == 'firefox')

  @property
  def browser_name(self):
    return self.target.capabilities['browserName']

  def bfs10426(self):
    return (self.browser_name == 'internet explorer')


@decorator
def bfs10433(bfs10104, *args, **bfs10430):
  try:
    return bfs10104(*args, **bfs10430)
  except URLError, bfs10432:
    if bfs10431(bfs10432):
      raise StaleElementReferenceException('The Selenium server this element belonged to is no longer available.')
    else:
      raise 



def bfs10431(bfs10435):
  try:
    bfs10434 = 10061
    return (bfs10435.args[0][0] == bfs10434)
  except (IndexError, TypeError):
    return False


@decorator
def bfs10443(bfs10104, self, *args, **bfs10436):
  if (not self.bfs10440):
    return bfs10104(self, *args, **bfs10436)

  try:
    return bfs10104(self, *args, **bfs10436)
  except StaleElementReferenceException, bfs10437:
    try:
      bfs10441 = bfs10442(self.target.parent)
      bfs10441.switch_to_frame(self.bfs10440)
    except NoSuchFrameException:
      raise bfs10437
    else:
      return bfs10104(self, *args, **bfs10436)



class bfs10444(object):
  def __init__(self, target, bfs10440=None):
    self.target = target
    self.bfs10440 = bfs10440
    self.bfs10445 = None

  @property
  @bfs10443
  @bfs10433
  def location(self):
    if (self.bfs10445 is None):
      location = self.target.location
      (x, y) = (location['x'], location['y'])
      size = self.target.size
      (width, height) = (size['width'], size['height'])
      self.bfs10445 = bfs10256(x, y, width, height)

    return self.bfs10445

  def is_displayed(self):
    try:
      return (self.target.is_displayed() and self.location.bfs10323(bfs10256(0, 0, sys.maxint, sys.maxint)))
    except StaleElementReferenceException:
      return False


  @bfs10443
  def get_attribute(self, bfs10447):
    return self.target.get_attribute(bfs10447)

  @property
  @bfs10443
  def text(self):
    return self.target.text

  @bfs10443
  def clear(self):
    self.target.clear()

  @bfs10443
  def send_keys(self, keys):
    self.target.send_keys(keys)

  @property
  @bfs10443
  def tag_name(self):
    return self.target.tag_name

  def unwrap(self):
    return self.target

  def __repr__(self):
    return ('<%s>%s</%s>' % (self.tag_name, self.target.text, self.tag_name))


class bfs10442(object):
  def __init__(self, driver, bfs10446=None):
    if (bfs10446 is None):
      bfs10446 = []

    self.driver = driver
    self.bfs10446 = bfs10446

  def __iter__(self):
    yield []
    for bfs10450 in xrange(sys.maxint):
      try:
        self.driver.switch_to.frame(bfs10450)
      except (StaleElementReferenceException, NoSuchFrameException):
        break
      else:
        bfs10452 = (self.bfs10446 + [bfs10450])
        for bfs10451 in bfs10442(self.driver, bfs10452):
          yield ([bfs10450] + bfs10451)

        try:
          self.switch_to_frame(self.bfs10446)
        except NoSuchFrameException:
          raise bfs10453()




  def switch_to_frame(self, bfs10454):
    self.driver.switch_to.default_content()
    for bfs10456 in bfs10454:
      self.driver.switch_to.frame(bfs10456)



class bfs10453(Exception):
  pass

