Python selenium.common.exceptions 模块,TimeoutException() 实例源码

我们从Python开源项目中,提取了以下49个代码示例,用于说明如何使用selenium.common.exceptions.TimeoutException()

项目:python3_crawl    作者:princewen    | 项目源码 | 文件源码
def login():
    """??????,??????????"""
    driver.get(start_url)
    wait = WebDriverWait(driver,10)
    try:
        username = wait.until(EC.presence_of_element_located((By.ID,"loginname")))
        password = wait.until(EC.presence_of_element_located((By.XPATH,'//*[@id="pl_login_form"]/div/div[3]/div[2]/div/input')))
        username.send_keys(user)
        password.send_keys(passwd)
        btn = wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="pl_login_form"]/div/div[3]/div[6]/a/span')))
        btn.click()
    except TimeoutException:
        print ("??")
        return
    except NoSuchElementException:
        print ("?????? ")
        return
项目:CrackWeiboSlide    作者:Python3WebSpider    | 项目源码 | 文件源码
def get_position(self):
        """
        ???????
        :return: ???????
        """
        try:
            img = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'patt-shadow')))
        except TimeoutException:
            print('??????')
            self.open()
        time.sleep(2)
        location = img.location
        size = img.size
        top, bottom, left, right = location['y'], location['y'] + size['height'], location['x'], location['x'] + size[
            'width']
        return (top, bottom, left, right)
项目:SerpScrap    作者:ecoron    | 项目源码 | 文件源码
def _wait_until_search_input_field_appears(self, max_wait=5):
        """Waits until the search input field can be located for the current search engine

        Args:
            max_wait: How long to wait maximally before returning False.

        Returns: False if the search input field could not be located within the time
                or the handle to the search input field.
        """

        def find_visible_search_input(driver):
            input_field = driver.find_element(*self._get_search_input_field())
            return input_field

        try:
            search_input = WebDriverWait(self.webdriver, max_wait).until(find_visible_search_input)
            return search_input
        except TimeoutException as e:
            logger.error('{}: TimeoutException waiting for search input field: {}'.format(self.name, e))
            return False
项目:SerpScrap    作者:ecoron    | 项目源码 | 文件源码
def _wait_until_search_param_fields_appears(self, max_wait=5):
        """Waits until the search input field contains the query.

        Args:
            max_wait: How long to wait maximally before returning False.
        """
        def find_visible_search_param(driver):
            for _, field in self._get_search_param_fields().items():
                input_field = driver.find_element(*field)
                if not input_field:
                    return False
            return True

        try:
            fields = WebDriverWait(self.webdriver, max_wait).until(find_visible_search_param)
            return fields
        except TimeoutException as e:
            logger.error('{}: TimeoutException waiting for search param field: {}'.format(self.name, e))
            return False
项目:SerpScrap    作者:ecoron    | 项目源码 | 文件源码
def _find_next_page_element(self):
        """Finds the element that locates the next page for any search engine.

        Returns:
            The element that needs to be clicked to get to the next page or a boolean value to
            indicate an error condition.
        """
        if self.search_type == 'normal':
            selector = self.next_page_selectors[self.search_engine_name]
            try:
                # wait until the next page link is clickable
                WebDriverWait(self.webdriver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, selector)))
            except (WebDriverException, TimeoutException):
                self._save_debug_screenshot()
                # raise Exception('{}: Cannot locate next page element: {}'.format(self.name, str(e)))
            try:
                return self.webdriver.find_element_by_css_selector(selector)
            except Exception:
                logger.error('failed find_element_by_css_selector, sleep 30 sec')
                time.sleep(30)
                pass

        elif self.search_type == 'image':
            self.page_down()
            if self.search_engine_name == 'google':
                return self.webdriver.find_element_by_css_selector('input._kvc')
            else:
                return True
项目:AutoTriageBot    作者:salesforce    | 项目源码 | 文件源码
def testGETOpenRedirect(url: str, cookies: Mapping[str, str]) -> Optional[str]:
    """ If the given URL redirects when accessed with the given cookies via GET, return the new URL, otherwise
        return None """
    driver = SeleniumDrivers.getFirefoxDriver()
    driver.setCookies(url, cookies)

    try:
        driver.get(url)

        time.sleep(config.timeout)

        if driver.current_url == url:
            driver.reset()
            return None
        else:
            url = driver.current_url
            driver.reset()
            return url
    except (TimeoutException, URLError):
        driver.reset()
        return None
项目:AutoTriageBot    作者:salesforce    | 项目源码 | 文件源码
def testPOSTOpenRedirect(url: str, cookies: Mapping[str, str], data: Mapping[str, str]) -> Optional[str]:
    """ If the given URL redirects when accessed with the given cookies via POST, return the new URL, otherwise
        return None """
    driver = SeleniumDrivers.getFirefoxDriver()
    driver.setCookies(url, cookies)
    try:
        driver.post(url, data)

        time.sleep(config.timeout)

        if driver.current_url == url:
            driver.reset()
            return None
        else:
            url = driver.current_url
            driver.reset()
            return url
    except (TimeoutException, URLError):
        driver.reset()
        return None
项目:AutoTriageBot    作者:salesforce    | 项目源码 | 文件源码
def testGETXSSDriver(url: str, cookies: Mapping[str, str], driver: webdriver) -> Optional[str]:
    """ If the given URL pops an alert box when accessed with the given cookies, return the contents of the alert box,
        otherwise return None """
    driver.setCookies(url, cookies)

    try:
        driver.get(url)

        WebDriverWait(driver, config.timeout).until(expected_conditions.alert_is_present())
        # Note that despite the name switch_to_alert also handles prompt:
        #   - http://selenium-python.readthedocs.io/navigating.html#popup-dialogs
        alert = driver.switch_to_alert()
        text = alert.text
        driver.reset()
        return text
    except (TimeoutException, URLError):
        driver.reset()
        return None
项目:AutoTriageBot    作者:salesforce    | 项目源码 | 文件源码
def testPOSTXSSDriver(url: str, cookies: Mapping[str, str], data: Mapping[str, str], driver: webdriver) -> \
        Optional[str]:
    """ If the given URL pops an alert box when accessed with the given cookies, return the contents of the alert box,
        otherwise return None """
    driver.setCookies(url, cookies)

    try:
        driver.post(url, data)

        WebDriverWait(driver, config.timeout).until(expected_conditions.alert_is_present())
        # Note that despite the name switch_to_alert also handles prompt:
        #   - http://selenium-python.readthedocs.io/navigating.html#popup-dialogs
        alert = driver.switch_to_alert()
        text = alert.text
        driver.reset()
        return text
    except (TimeoutException, URLError):
        driver.reset()
        return None
项目:AutoTriageBot    作者:salesforce    | 项目源码 | 文件源码
def retry(func: Callable[[], T]) -> T:
    """ Retry the function with 30 second timeouts until it works
        - I've observed the getFirefoxDriver() without this freeze once (out of hundreds of runs...) so adding this
          as a safety measure. """
    for i in range(10):
        if config.DEBUG and i > 0:
            print("Retry #%s" % str(i))

        def timeoutHandler(signum, frame):
            raise TimeoutException("Timeout!")
        signal.signal(signal.SIGALRM, timeoutHandler)
        signal.alarm(delayTime)
        try:
            t = func()
            signal.alarm(0)
            return t
        except TimeoutException:
            pass
    signal.alarm(0)
    raise TimeoutException("Retried 10 times... Failed!")
项目:devsecops-example-helloworld    作者:boozallen    | 项目源码 | 文件源码
def validateLink(self, url, expectedText = None, expectedTitle = None, expectedUrl = None, \
        xpathContext = None):
        xpathContext = xpathContext if xpathContext else ""
        selector = ".//{1}a[@href='{0}']".format(url, xpathContext)
        if expectedText:
            self.assertTextPresent(By.XPATH, selector, expectedText)

        totalTries = self._followLinkMaxRetries + 1
        for i in range(0, totalTries):
            try:
                self.click(By.XPATH, selector, "Click {0}".format(expectedText), \
                    expectedURL = expectedUrl if expectedUrl else url)
                break
            except TimeoutException:
                retry = i + 1 # First 'try' is not a retry
                if retry < totalTries:
                    pass
        if expectedTitle:
            self.assertTitle(expectedTitle);
项目:devsecops-example-helloworld    作者:boozallen    | 项目源码 | 文件源码
def until(self, method, message=''):
        """Calls the method provided with the driver as an argument until the \
        return value is not False."""
        screen = None
        stacktrace = None

        end_time = time.time() + self._timeout
        while True:
            try:
                value = method(self._driver)
                if value:
                    return value
            except self._ignored_exceptions as exc:
                screen = getattr(exc, 'screen', None)
                stacktrace = getattr(exc, 'stacktrace', None)
            time.sleep(self._poll)
            if time.time() > end_time:
                break
        raise TimeoutException(message, screen, stacktrace)
项目:warriorframework    作者:warriorframework    | 项目源码 | 文件源码
def wait_until_element_is_clickable(self, browser_instance, locator_type, locator,
                                        timeout=5):
        try:
            WebDriverWait(browser_instance, int(timeout)).until(EC.element_to_be_clickable((BYCLASS[locator_type.strip().upper()], locator)))
            status = True
        except KeyError:
            print_error("The given locator_type - '{0}' does not match any of "
                        "the accepted locator_types.".format(locator_type))
            print_error("{0}, {1}, {2}, {3}, {4}, {5}, {6}, and {7} are the "
                        "accepted locator types.".format("id", "xpath", "link"
                                                         "class", "tag", "name",
                                                         "css_selector",
                                                         "partial_link"))
            status = "ERROR"
        except TimeoutException:
            print_error("Element unclickable after {0} seconds".format(timeout))
            status = False
        except Exception as e:
            print_error("An Exception Ocurred: {0}".format(e))
            status = "ERROR"
        return status
项目:warriorframework    作者:warriorframework    | 项目源码 | 文件源码
def wait_until_presence_of_element_located(self, browser_instance,
                                               locator_type, locator, timeout=5):
        try:
            WebDriverWait(browser_instance, int(timeout)).until(EC.presence_of_element_located((BYCLASS[locator_type.strip().upper()], locator)))
            status = True
        except KeyError:
            print_error("The given locator_type - '{0}' does not match any of "
                        "the accepted locator_types.".format(locator_type))
            print_error("{0}, {1}, {2}, {3}, {4}, {5}, {6}, and {7} are the "
                        "accepted locator types.".format("id", "xpath", "link"
                                                         "class", "tag", "name",
                                                         "css_selector",
                                                         "partial_link"))
            status = "ERROR"
        except TimeoutException:
            print_error("Element not present after {0} seconds".format(timeout))
            status = False
        except Exception as e:
            print_error("An Exception Ocurred: {0}".format(e))
            status = "ERROR"
        return status
项目:warriorframework    作者:warriorframework    | 项目源码 | 文件源码
def wait_until_presence_of_all_elements_located(self, browser_instance,
                                                    locator_type, locator, timeout=5):
        try:
            WebDriverWait(browser_instance, int(timeout)).until(EC.presence_of_all_elements_located((BYCLASS[locator_type.strip().upper()], locator)))
            status = True
        except KeyError:
            print_error("The given locator_type - '{0}' does not match any of "
                        "the accepted locator_types.".format(locator_type))
            print_error("{0}, {1}, {2}, {3}, {4}, {5}, {6}, and {7} are the "
                        "accepted locator types.".format("id", "xpath", "link"
                                                         "class", "tag", "name",
                                                         "css_selector",
                                                         "partial_link"))
            status = "ERROR"
        except TimeoutException:
            print_error("Elements not present after {0} seconds".format(timeout))
            status = False
        except Exception as e:
            print_error("An Exception Ocurred: {0}".format(e))
            status = "ERROR"
        return status
项目:warriorframework    作者:warriorframework    | 项目源码 | 文件源码
def wait_until_visibility_of_element_located(self, browser_instance,
                                                      locator_type, locator,
                                                      timeout=5):
        try:
            WebDriverWait(browser_instance, int(timeout)).until(EC.visibility_of_element_located((BYCLASS[locator_type.strip().upper()], locator)))
            status = True
        except KeyError:
            print_error("The given locator_type - '{0}' does not match any of "
                        "the accepted locator_types.".format(locator_type))
            print_error("{0}, {1}, {2}, {3}, {4}, {5}, {6}, and {7} are the "
                        "accepted locator types.".format("id", "xpath", "link"
                                                         "class", "tag", "name",
                                                         "css_selector",
                                                         "partial_link"))
            status = "ERROR"
        except TimeoutException:
            print_error("Element not visible after {0} seconds".format(timeout))
            status = False
        except Exception as e:
            print_error("An Exception Ocurred: {0}".format(e))
            status = "ERROR"
        return status
项目:Dallinger    作者:Dallinger    | 项目源码 | 文件源码
def participate(self):
        """Finish reading and send text"""
        try:
            logger.info("Entering participate method")
            ready = WebDriverWait(self.driver, 10).until(
                EC.element_to_be_clickable((By.ID, 'finish-reading')))
            stimulus = self.driver.find_element_by_id('stimulus')
            story = stimulus.find_element_by_id('story')
            story_text = story.text
            logger.info("Stimulus text:")
            logger.info(story_text)
            ready.click()
            submit = WebDriverWait(self.driver, 10).until(
                EC.element_to_be_clickable((By.ID, 'submit-response')))
            textarea = WebDriverWait(self.driver, 10).until(
                EC.element_to_be_clickable((By.ID, 'reproduction')))
            textarea.clear()
            text = self.transform_text(story_text)
            logger.info("Transformed text:")
            logger.info(text)
            textarea.send_keys(text)
            submit.click()
            return True
        except TimeoutException:
            return False
项目:kolla-kubernetes    作者:openstack    | 项目源码 | 文件源码
def setUp(self):
        self.driver = self.getDriver()
        print("Got driver")
        self.driver.get(os.environ["OS_HORIZON"])
        try:
            delay = 5
            element_present = EC.presence_of_element_located((By.NAME,
                                                              'username'))
            WebDriverWait(self.driver, delay).until(element_present)
            self.driver.title.index("Login - OpenStack Dashboard")
            elem = self.driver.find_element_by_name("username")
            elem.send_keys(os.environ["OS_USERNAME"])
            elem = self.driver.find_element_by_name("password")
            elem.send_keys(os.environ["OS_PASSWORD"])
            elem.send_keys(Keys.RETURN)
        except TimeoutException:
            print("Loading took too much time!")
            raise
        time.sleep(2)
项目:flasky    作者:RoseOu    | 项目源码 | 文件源码
def until(self, method, message=''):
        """Calls the method provided with the driver as an argument until the \
        return value is not False."""
        screen = None
        stacktrace = None

        end_time = time.time() + self._timeout
        while True:
            try:
                value = method(self._driver)
                if value:
                    return value
            except self._ignored_exceptions as exc:
                screen = getattr(exc, 'screen', None)
                stacktrace = getattr(exc, 'stacktrace', None)
            time.sleep(self._poll)
            if time.time() > end_time:
                break
        raise TimeoutException(message, screen, stacktrace)
项目:alfred-alarmcom    作者:schwark    | 项目源码 | 文件源码
def state(self):
        """
        Check the current status of the alarm system.
        """
        # Click the refresh button to verify the state if it was made somewhere else
        try:
            # Recheck the current status
            self._driver.get(self._driver.current_url)
            current_status = WebDriverWait(self._driver, self.timeout).until(EC.presence_of_element_located((self.STATUS_IMG[0],
                                                   self.STATUS_IMG[1]))).text
            _LOGGER.debug('Fetched current status from system: {}'.format(current_status))
            return current_status
        except (exceptions.NoSuchElementException, exceptions.NoSuchWindowException, exceptions.TimeoutException, urllib.error.URLError) as e:
            _LOGGER.warning('Error while checking alarm status. Attempting login again.')
            self._login()
            current_status = WebDriverWait(self._driver, self.timeout).until(EC.presence_of_element_located((self.STATUS_IMG[0],
                                                   self.STATUS_IMG[1]))).text
            return current_status
项目:spider-lianjia    作者:longxiaofei    | 项目源码 | 文件源码
def get_total_page(browser, url):
    browser.get(url)
    try:
        time.sleep(4)
        total_room = browser.find_element_by_xpath('/html/body/div[4]/div[1]/div[2]/h2/span').text
        if not total_room:
            return None
        if int(total_room) <= 30:
            return 1
        total = WebDriverWait(browser, 30).until(
                EC.presence_of_element_located((By.XPATH, "/html/body/div[4]/div[1]/div[7]/div[2]/div/a[last()]"))
            )
        if not total.text.isdigit():
            total_page = browser.find_element_by_xpath('/html/body/div[4]/div[1]/div[7]/div[2]/div/a[last()-1]').text
        else:
            total_page = total.text
        return total_page
    except TimeoutException as e:
        print('????????25??????')
        time.sleep(25)
        return get_total_page(url)
项目:CartierEJ    作者:haifengrundadi    | 项目源码 | 文件源码
def until_not(self, method, message=''):
        """
            Calls the method provided with the driver as an argument until the \
            return value is False.
        """
        end_time = time.time() + self._timeout
        while True:
            try:
                value = method(self._driver)
                if not value:
                    return value
            except self._ignored_exceptions:
                return True
            time.sleep(self._poll)
            if time.time() > end_time:
                break
        raise TimeoutException(message)
项目:leetcode    作者:thomasyimgit    | 项目源码 | 文件源码
def until(self, method, message=''):
        """Calls the method provided with the driver as an argument until the \
        return value is not False."""
        screen = None
        stacktrace = None

        end_time = time.time() + self._timeout
        while True:
            try:
                value = method(self._driver)
                if value:
                    return value
            except self._ignored_exceptions as exc:
                screen = getattr(exc, 'screen', None)
                stacktrace = getattr(exc, 'stacktrace', None)
            time.sleep(self._poll)
            if time.time() > end_time:
                break
        raise TimeoutException(message, screen, stacktrace)
项目:WebDriverAgent-python-client    作者:chenhengjie123    | 项目源码 | 文件源码
def wait_activity(self, activity, timeout, interval=1):
        """Wait for an activity: block until target activity presents
        or time out.

        This is an Android-only method.

        :Agrs:
         - activity - target activity
         - timeout - max wait time, in seconds
         - interval - sleep interval between retries, in seconds
        """
        try:
            WebDriverWait(self, timeout, interval).until(
                lambda d: d.current_activity == activity)
            return True
        except TimeoutException:
            return False
项目:E-commerce-crawlers    作者:Hopetree    | 项目源码 | 文件源码
def get_info(self,id):
        '''???????????????????????????????'''
        dic = {}
        url = 'https://detail.tmall.hk/hk/item.htm?spm=a220o.1000855.1998025129.2.102f616emYvWTL&abtest=_AB-LR32-PR32&pvid=a2be15e3-8200-46b3-bd09-de041b8b8dc3&pos=2&abbucket=_AB-M32_B9&acm=03054.1003.1.2431317&id={}&scm=1007.12144.81309.23864_0&sku_properties=-1:-1'.format(id)
        self.driver.get(url)
        # html = self.driver.page_source
        # print(html)
        try:
            location = self.waiter.until(
                EC.presence_of_element_located((By.XPATH,'//dl[@class="tm-happy11-panel"]/dd/span[@class="tm-price"]'))
            )
            info = location.text.strip()
            dic['id'] = id
            dic['price'] = info if info else '?????'
            self.write_info(dic)
        except TimeoutException as e:
            print(e)
            dic['id'] = id
            dic['price'] = '{}????????'.format(e).strip()
            self.write_info(dic)
项目:Phantomime    作者:psyb0t    | 项目源码 | 文件源码
def wait_for_alert(t=3):
  """Wait for an alert box to be present

  Arguments:
  t -- the timeout in seconds (default = 3)

  Returns:
  The alert Selenium object or False
  """
  if verbose:
    print 'Waiting for alert to pop...'

  try:
    WebDriverWait(Browser, t).until(EC.alert_is_present())

    return Browser.switch_to_alert()
  except TimeoutException:
    return False
项目:Funda-Scraper    作者:Weesper1985    | 项目源码 | 文件源码
def enter_search_term(browser, search_term):

    wait = WebDriverWait(browser, 10)

    try:
        search_bar = wait.until(EC.presence_of_element_located(
            (By.XPATH, "//input[@id='autocomplete-input']")))
        button = wait.until(EC.element_to_be_clickable(
            (By.XPATH, "//button[@class='button-primary-alternative']")))
        search_bar.click()
        time.sleep(randint(10, 15))
        search_bar.clear()
        time.sleep(randint(10, 15))
        search_bar.send_keys(search_term)
        time.sleep(randint(10, 15))
        button.click()
        print("search-button has been clicked")
        time.sleep(randint(15, 20))
        return True
    except (TimeoutException, NoSuchElementException) as e:
        print(str(e))
        return False

# Scrape the resulting page and move on to the next page until hitting the predefined lastpage. All results are stored in a csv-file
项目:python3_crawl    作者:princewen    | 项目源码 | 文件源码
def search_mainpage(query,times=0):
    try:
        baseurl = 'http://www.tianyancha.com/search?'
        params = {
            'key':query,
            'checkForm':'searchBox'
        }
        url = baseurl + urllib.parse.urlencode(params)
        print (url)
        driver.get(url)
        wait = WebDriverWait(driver, 10)
        try:
            href = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="ng-view"]/div[2]/div/div/div[1]/div[3]/div[1]/div[2]/div[1]/div[1]/a'))).get_attribute('href')
        except Exception as e:
            print ('??????')
            href = ''
        return href

    except TimeoutException:
        print ('crawl again')
        if times < 5:
            times+=1
            search_mainpage(query,times)
        else:
            return ''
项目:weiboCrawler    作者:hjydzh    | 项目源码 | 文件源码
def login_by_cookie(browser):
    print '????'
    try:
        browser.get('http://weibo.com/')
    except TimeoutException:
        print '???????????'
        browser.execute_script('window.stop()')
    browser.execute_script('window.stop()')
    print('????')
    time.sleep(5)
    browser.execute_script('window.stop()')
    browser.delete_all_cookies()
    print('????cookies')
    for cookie in pickle.load(open('cookie')):
        try:
            browser.add_cookie(cookie)
        except:
            pass
    print('????cookies')
    browser.get('http://m.weibo.cn/mblog')
    time.sleep(5)
    print '????:'
    print browser.title
    if '????' in browser.title.encode('utf-8'):
        pickle.dump(browser.get_cookies(), open('cookie', 'wb'))
项目:ShuoshuoMonitor    作者:aploium    | 项目源码 | 文件源码
def until(self, method, message=''):
        """Calls the method provided with the driver as an argument until the \
        return value is not False."""
        screen = None
        stacktrace = None

        end_time = time.time() + self._timeout
        while True:
            try:
                value = method(self._driver)
                if value:
                    return value
            except self._ignored_exceptions as exc:
                screen = getattr(exc, 'screen', None)
                stacktrace = getattr(exc, 'stacktrace', None)
            time.sleep(self._poll)
            if time.time() > end_time:
                break
        raise TimeoutException(message, screen, stacktrace)
项目:snowballing    作者:JoaoFelipe    | 项目源码 | 文件源码
def check_captcha(driver, condition):
    """ Check if it expects a captcha """
    captcha = driver.find_elements_by_css_selector("#captcha")
    captcha += driver.find_elements_by_css_selector("#gs_captcha_f")
    captcha += driver.find_elements_by_css_selector("#g-recaptcha")
    captcha += driver.find_elements_by_css_selector("#recaptcha")
    while captcha:
        print("Ops. It requires a captcha!")
        print("If you filled in the browser, type '<ok>' here.")
        inp = input("Captcha: ")
        if inp == "<ok>":
            break
        captcha[0].send_keys(inp)
        captcha[0].send_keys(Keys.RETURN)
        try:
            if condition:
                wait_for(driver, condition)
            break
        except TimeoutException:
            captcha = driver.find_elements_by_css_selector("#captcha")
项目:amazon_order_history_scraper    作者:drewctate    | 项目源码 | 文件源码
def until(self, method, message=''):
        """Calls the method provided with the driver as an argument until the \
        return value is not False."""
        screen = None
        stacktrace = None

        end_time = time.time() + self._timeout
        while True:
            try:
                value = method(self._driver)
                if value:
                    return value
            except self._ignored_exceptions as exc:
                screen = getattr(exc, 'screen', None)
                stacktrace = getattr(exc, 'stacktrace', None)
            time.sleep(self._poll)
            if time.time() > end_time:
                break
        raise TimeoutException(message, screen, stacktrace)
项目:CookiesPool    作者:Python3WebSpider    | 项目源码 | 文件源码
def get_position(self):
        """
        ???????
        :return: ???????
        """
        try:
            img = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'patt-shadow')))
        except TimeoutException:
            print('??????')
            self.open()
        time.sleep(2)
        location = img.location
        size = img.size
        top, bottom, left, right = location['y'], location['y'] + size['height'], location['x'], location['x'] + size[
            'width']
        return (top, bottom, left, right)
项目:webapp    作者:superchilli    | 项目源码 | 文件源码
def until(self, method, message=''):
        """Calls the method provided with the driver as an argument until the \
        return value is not False."""
        screen = None
        stacktrace = None

        end_time = time.time() + self._timeout
        while True:
            try:
                value = method(self._driver)
                if value:
                    return value
            except self._ignored_exceptions as exc:
                screen = getattr(exc, 'screen', None)
                stacktrace = getattr(exc, 'stacktrace', None)
            time.sleep(self._poll)
            if time.time() > end_time:
                break
        raise TimeoutException(message, screen, stacktrace)
项目:cyphon    作者:dunbarcyber    | 项目源码 | 文件源码
def count(self):
        """

        """
        time.sleep(SLEEP)
        self.click()
        time.sleep(SLEEP)  # wait for server response
        option_path = self.path + '/span[@data-value]'
        try:
            WebDriverWait(self.driver, TIMEOUT).until(
                EC.presence_of_element_located((By.XPATH, option_path))
            )
            options = self.driver.find_elements_by_xpath(option_path)
            return len(options)
        except (TimeoutException, socket.timeout):
            return 0
项目:spider    作者:luanxiangming    | 项目源码 | 文件源码
def next_page(page_number):
    print("---????: " + str(page_number))
    try:
        input = wait.until(
            EC.presence_of_element_located((By.CSS_SELECTOR, "#J_bottomPage > span.p-skip > input"))
        )
        submit = wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, "#J_bottomPage > span.p-skip > a"))
        )
        input.clear()
        input.send_keys(page_number)
        submit.click()
        wait.until(
            EC.text_to_be_present_in_element(
                (By.CSS_SELECTOR, '#J_bottomPage > span.p-num > a.curr'), str(page_number))
        )
        get_products()
    except TimeoutException:
        next_page(page_number)
项目:spider    作者:luanxiangming    | 项目源码 | 文件源码
def search():
    print("---?????: " + KEY_WORD_TAOBAO)
    print('os.path: ' + str(os.path))
    try:
        browser.get('https://www.taobao.com')

        input = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#q')))
        submit = wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, '#J_TSearchForm > div.search-button > button'))
        )
        input.send_keys(KEY_WORD_TAOBAO)
        submit.click()
        total = wait.until(
            EC.presence_of_element_located((By.CSS_SELECTOR, '#mainsrp-pager > div > div > div > div.total'))
        )
        get_products()
        return total.text
    except TimeoutException:
        return search()
项目:spider    作者:luanxiangming    | 项目源码 | 文件源码
def repeat_next(self):
        while True:
            try:
                next_button = self.wait.until(
                    EC.element_to_be_clickable(
                        (By.CLASS_NAME, 'float-right'))
                )
                pages = self.driver.find_elements(By.CSS_SELECTOR, '.current.reference.internal')
                for page in pages:
                    ("\n" + page.get_attribute('text'))
                self.click(next_button)
            except NoSuchElementException:
                print("End of page")
                break
            except TimeoutException:
                print("Timeout error...")
                break
项目:CAM2RetrieveData    作者:PurdueCAM2Project    | 项目源码 | 文件源码
def click_all_button(self):
        """ click the button that redirects to the list of all webcams

            Raises:
                UnexpectedAlertPresentException: it occurs when a ramdom alert dialog pops up
                TimeoutException: it occurs when the element we want is never loaded
        """
        try:
            # wait until the element is loaded
            self.wait.until(lambda driver: driver.find_element_by_id("VERZEICHNIS-button_alle"))

            # get the button element and click it
            button = self.driver.find_element_by_id("VERZEICHNIS-button_alle")
            button.click()

        except UnexpectedAlertPresentException:
            self.handle_alert()
            self.click_all_button()

        except:
            self.driver.refresh()
            self.click_all_button()
项目:lagou_spider    作者:longxiaofei    | 项目源码 | 文件源码
def get_index():
    try:
        print('????')
        browser.get('https://www.lagou.com/')
        close_window = wait.until(
                EC.element_to_be_clickable((By.CSS_SELECTOR, '#cboxClose'))
            )
        close_window.click()
        time.sleep(2)
        search_place = wait.until(
                EC.presence_of_element_located((By.CSS_SELECTOR, '#search_input'))
            )
        search_button = wait.until(
                EC.element_to_be_clickable((By.CSS_SELECTOR, '#search_button'))
            )
        search_place.send_keys(WORK)
        time.sleep(2)
        search_button.click()
    except TimeoutException as e:
        print('?????')
        login()
项目:InstagramLocationScraper    作者:VoodaGod    | 项目源码 | 文件源码
def scrapeLocation(self, location, dateTo, dateFrom, maxPosts):
        """return number of posts at location since dateFrom till dateTo, scrolling at most to maxPosts"""
        self.timeLimit = datetime.now() + timedelta(minutes=TIMEOUT_MINUTES)
        attempts = 0
        while(attempts < 3): #have to restart scrape if clicked on deleted post
            if(attempts > 0):
                print("\nrestarting scrape of " + location + ", attempt " + str(attempts) + "\n")
            try:
                self.browseLocationPage(location)
                try:
                    postList = self.scrollToDate(dateFrom, maxPosts)
                    if(len(postList) <= 9): #no recent posts, only "top posts" or none
                        return 0
                    firstPostIndex = self.findFirstPost(dateFrom, postList)
                    lastPostIndex = self.findLastPost(dateTo, postList, firstPostIndex)
                except TimeoutException:
                    print("scraper " + location + " timed out")
                    return ERROR_INDICATOR
                if(len(postList) == (maxPosts + 9)): #didn't finish scrolling
                    return -(firstPostIndex - lastPostIndex) #negative = more than
                else:
                    return (firstPostIndex - lastPostIndex)
            except self.PageNotFoundException:
                attempts += 1
项目:Twitch-Farmer    作者:MD3XTER    | 项目源码 | 文件源码
def Logged(twitchUsername, twitchPassword):
    global oldProxy

    #checks if logged
    try:
        WebDriverWait(driver, delay - 5).until(EC.presence_of_element_located((By.ID, searchFieldID)))
        Follow()
    except TimeoutException:
        try:
            driver.find_element_by_xpath(incorrectCapthaXpath)
            print ("Incorrect CAPTCHA, changing proxy!!!" + '\n')
            print("------------------------------------------- " + '\n')
            change_proxy(twitchUsername, twitchPassword)          
        except:
            try:
                driver.find_element_by_xpath(incorrectUserXpath)
                print ("Incorrect username or password, changing account!!!" + '\n')
                print("------------------------------------------- " + '\n')
                oldProxy = proxies[proxy_count]              
                change_Account = True
                driver.quit()
            except:
                    print ("Time expired, changing proxy!!!(2)" + '\n')
                    print("------------------------------------------- " + '\n')
                    change_proxy(twitchUsername, twitchPassword)
项目:badger-claw    作者:cowlicks    | 项目源码 | 文件源码
def crawl(timeout=7, n_urls=len(top500.urls), start=0):
    logger.info('starting new crawl with timeout %s n_urls %s start %s' % (timeout, n_urls, start))
    with xvfb_manager():
        driver = start_driver()
        driver.set_page_load_timeout(timeout)
        driver.set_script_timeout(timeout)

        for url in top500.urls[start:start+n_urls]:
            try:
                logger.info('visiting %s' % url)
                driver.get(url)
                sleep(timeout)
            except TimeoutException as e:
                logger.info('timeout on %s ' % url)
                driver = timeout_workaround(driver)
                continue
        data = dump_data(driver)
        driver.quit()
        return data
项目:brush    作者:chenshiyang2015    | 项目源码 | 文件源码
def click_search_url(cls, items):

        content = env.threadlocal.BROWSER.find_element_by_css_selector("div[id=\"content_left\"]")
        links = content.find_elements_by_tag_name("a")
        i = 0

        for link in links:
            if link.get_attribute('class') == "c-showurl":
                if i in items:
                    log.step_normal("????item: [%s]" % i)
                    log.step_normal("????url?[%s], ??????: [%s]" % (link.get_attribute('href'), link.text))

                    try:
                        link.click()
                        random.randint(3, 7)
                        WebBrowser.SwitchToNewPopWindow()
                        WebBrowser.SwitchToDefaultWindow()
                        log.step_normal('?????????')
                    except (TimeoutException, WebDriverException) as e:
                        log.step_normal('??????---> %s' % e)
                        WebBrowser.SwitchToDefaultWindow()

                i = i + 1

    #??????????????
项目:AutoTestFramework    作者:huilansame    | 项目源码 | 文件源码
def clk(driver, url):
    """locate and click url"""
    locator = (By.XPATH, '//span[contains(text(), "{0}")]/../../../../a'.format(url))
    try:
        WebDriverWait(driver, 5, 0.5).until(EC.element_to_be_clickable(locator))
        try:
            moved_to_element = driver.find_element_by_xpath('//span[contains(text(), "{0}")]'.format(url))
            target_element = driver.find_element(*locator)
            ActionChains(driver).move_to_element(to_element=moved_to_element).click(target_element).perform()
            sleep(1)
            if 'm.baidu.com' in driver.current_url:
                target_element.click()
            return 1
        except:
            return 0
    except TimeoutException:
        return 0
项目:selenext    作者:Wykleph    | 项目源码 | 文件源码
def wait_exists(self, timeout=None):
        """
        Wait for the element to appear in the DOM.

        Args:
            timeout:
                None or int

        Returns:
            self
        """

        wait_time = 0

        while not self.exists():
            sleep(1)
            wait_time += 1
            if timeout is not None:
                if wait_time >= timeout:
                    raise TimeoutException()
        return self
项目:new_media_fans_cralwer    作者:NiShuang    | 项目源码 | 文件源码
def get_by_selenium():
    url = 'https://www.youtube.com/channel/UC3qWcF49rv8VMZO7Vg6kj5w'
    cap = webdriver.DesiredCapabilities.PHANTOMJS
    cap["phantomjs.page.settings.resourceTimeout"] = 1000
    cap["phantomjs.page.settings.loadImages"] = False
    cap["phantomjs.page.settings.localToRemoteUrlAccessEnabled"] = True
    cap["userAgent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0"
    cap["XSSAuditingEnabled"] = True
    driver = webdriver.PhantomJS(desired_capabilities=cap,
                                      service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any',
                                                    '--web-security=true'])
    # driver = webdriver.Chrome()
    driver.get(url)
    wait = WebDriverWait(driver, 20)
    # print driver.page_source
    try:
        fans = int(wait.until(lambda x: x.find_elements_by_class_name('yt-subscription-button-subscriber-count-branded-horizontal')[0].text))
    except TimeoutException:
        fans = 0
    print fans
    driver.quit()
    return fans
项目:new_media_fans_cralwer    作者:NiShuang    | 项目源码 | 文件源码
def get_by_selenium():
    url = 'http://i.youku.com/i/UMjk1ODg3NDgwOA=='
    cap = webdriver.DesiredCapabilities.PHANTOMJS
    cap["phantomjs.page.settings.resourceTimeout"] = 1000
    cap["phantomjs.page.settings.loadImages"] = False
    cap["phantomjs.page.settings.localToRemoteUrlAccessEnabled"] = True
    cap["userAgent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0"
    cap["XSSAuditingEnabled"] = True
    driver = webdriver.PhantomJS(desired_capabilities=cap,
                                      service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any',
                                                    '--web-security=true'])
    # driver = webdriver.Chrome()
    driver.get(url)
    wait = WebDriverWait(driver, 20)
    print driver.page_source
    try:
        fans = int(wait.until(lambda x: x.find_elements_by_class_name('snum')[0].find_element_by_xpath('em').text))
    except TimeoutException:
        fans = 0
    print fans
    driver.quit()
    return fans
项目:picoCTF    作者:picoCTF    | 项目源码 | 文件源码
def test_problems_accesible(self):
        """
        Tests that at least one problem is accessible from the problems page.
        """

        self.driver.get(BASE_URI+"problems")

        try:
            some_problem_description = self.find_class("problem-description")
        except TimeoutException as e:
            assert False, "No problems are visible to a new user."