Python selenium.webdriver.support.wait.WebDriverWait() Examples

The following are 30 code examples of selenium.webdriver.support.wait.WebDriverWait(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module selenium.webdriver.support.wait , or try the search function .
Example #1
Source File: screenshot.py    From AboveTustin with MIT License 8 votes vote down vote up
def loadmap(self):
        '''
        loadmap()
        Creates a browser object and loads the webpage.
        It sets up the map to the proper zoom level.

        Returns the browser on success, None on fail.
        '''
        browser = webdriver.PhantomJS(desired_capabilities={'phantomjs.page.settings.resourceTimeout': '20000'})
        browser.set_window_size(abovetustin_image_width, abovetustin_image_height)

        print("getting web page {}".format(self.url))
        browser.set_page_load_timeout(15)
        browser.get(self.url)

        # Need to wait for the page to load
        timeout = g_request_timeout
        print ("waiting for page to load...")
        wait = WebDriverWait(browser, timeout)
        element = wait.until(EC.element_to_be_clickable((By.CLASS_NAME,'vrsMenu')))
        self.browser = browser 
Example #2
Source File: spider_weixun_by_sogou.py    From WeixinSpider with Apache License 2.0 7 votes vote down vote up
def get_selenium_js_html(url):
    # browser = webdriver.PhantomJS(executable_path=r'D:\Python2.7\Scripts\phantomjs.exe')
    options = Options()
    options.add_argument('-headless')  # 无头参数
    driver = Chrome(executable_path='chromedriver', chrome_options=options)
    wait = WebDriverWait(driver, timeout=10)

    driver.get(url)
    time.sleep(3)
    # 执行js得到整个页面内容
    html = driver.execute_script("return document.documentElement.outerHTML")
    driver.close()
    return html


# 获取公众号文章内容 
Example #3
Source File: common.py    From bitmask-dev with GNU General Public License v3.0 7 votes vote down vote up
def _wait_until_elements_are_visible_by_locator(context, locator_tuple,
                                                timeout=TIMEOUT_IN_S):
    wait = WebDriverWait(context.browser, timeout)
    wait.until(EC.presence_of_all_elements_located(locator_tuple))
    return context.browser.find_elements(locator_tuple[0], locator_tuple[1]) 
Example #4
Source File: Autoticket.py    From Autoticket with MIT License 7 votes vote down vote up
def enter_concert(self):
        self.login()
        try:
            if self.type == 1:  # detail.damai.cn
                locator = (By.XPATH, "/html/body/div[1]/div/div[3]/div[1]/a[2]/div")
            elif self.type == 2:  # piao.damai.cn
                locator = (By.XPATH, "/html/body/div[1]/div/ul/li[2]/div/label/a[2]")
            WebDriverWait(self.driver, self.total_wait_time, self.refresh_wait_time).until(
                EC.text_to_be_present_in_element(locator, self.nick_name))
            self.status = 1
            print("###登录成功###")
        except Exception as e:
            print(e)
            self.status = 0
            self.driver.quit()
            raise Exception("***错误:登录失败,请检查配置文件昵称或删除cookie.pkl后重试***") 
Example #5
Source File: __init__.py    From selenium-python-helium with MIT License 7 votes vote down vote up
def wait_until_impl(self, condition_fn, timeout_secs=10, interval_secs=0.5):
		if ismethod(condition_fn):
			is_bound = condition_fn.__self__ is not None
			args_spec = getfullargspec(condition_fn).args
			unfilled_args = len(args_spec) - (1 if is_bound else 0)
		else:
			if not isfunction(condition_fn):
				condition_fn = condition_fn.__call__
			args_spec = getfullargspec(condition_fn).args
			unfilled_args = len(args_spec)
		condition = \
			condition_fn if unfilled_args else lambda driver: condition_fn()
		wait = WebDriverWait(
			self.require_driver().unwrap(), timeout_secs,
			poll_frequency=interval_secs
		)
		wait.until(condition) 
Example #6
Source File: legoshop.py    From lego-mindstorms-ev3-comparison with GNU General Public License v3.0 7 votes vote down vote up
def _init_browser(self, browser, url_path=""):
        """
        Open browser with LEGO shop URL (index page if path not set)
        """
        self.shop_url = "https://www.lego.com/%s" % url_path

        self._load_driver(browser)

        # Selenium can't find some elements otherwise
        self.browser.maximize_window()

        self.browser.get(self.shop_url)

        # will wait to 5 sec for and ExpectedCondition success,
        # otherwise exception TimeoutException
        self.wait = WebDriverWait(self.browser, 5)

        self.browser_info() 
Example #7
Source File: scrape_espncricinfo.py    From Awesome-Scripts with MIT License 7 votes vote down vote up
def get_latest_wallpapers():
    browser = webdriver.PhantomJS(PHANTOMJS_PATH, service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any'])
    today_date = time.strftime("%d+%b+%Y")  
    yesterday = datetime.now() - timedelta(days=1)
    yesterday_date = yesterday.strftime('%d+%b+%Y')
    first_page_url = 'http://www.espncricinfo.com/ci/content/image/?datefrom='+yesterday_date+'&dateupto='+today_date+';'
    browser.get(first_page_url)
    wait = WebDriverWait(browser, 10)
    wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "img-wrap")))
    time.sleep(2)
    # let's parse our html
    soup = BeautifulSoup(browser.page_source, "html.parser")
    images = soup.find_all('div', class_='picture')
    for image in images:
        url = "http://www.espncricinfo.com/" + image.find('a').get('href')
        print(url) 
Example #8
Source File: browser.py    From Weibo-Album-Crawler with MIT License 5 votes vote down vote up
def _wait_element(self, xpath, timeout=60):
        locator = (By.XPATH, xpath)
        condition = visibility_of_element_located(locator)
        element = WebDriverWait(self._driver, 60).until(condition)
        return element 
Example #9
Source File: test_search.py    From hepdata with GNU General Public License v2.0 5 votes vote down vote up
def test_author_search(live_server, env_browser):
    """Test the author search"""
    browser = env_browser

    # Go to the search page
    browser.get(flask.url_for('es_search.search', _external=True))

    # Find the author search box
    search_input = browser.find_element_by_css_selector('#author-suggest')
    search_input.send_keys('ada')

    # Wait for search results to appear
    WebDriverWait(browser, 10).until(
        EC.presence_of_element_located((By.CLASS_NAME, 'tt-suggestion'))
    )

    # Check author results are as we expect
    search_results = browser.find_elements_by_class_name('tt-suggestion')
    expected_authors = [
        'Falkowski, Adam',
        'Lyon, Adam Leonard'
    ]

    assert(len(search_results) == len(expected_authors))

    for i, result in enumerate(search_results):
        assert(result.text == expected_authors[i]) 
Example #10
Source File: test_payment.py    From ecommerce with GNU Affero General Public License v3.0 5 votes vote down vote up
def assert_browser_on_receipt_page(self, selenium):
        WebDriverWait(selenium, 20).until(
            EC.visibility_of_element_located((By.ID, 'receipt-container'))
        ) 
Example #11
Source File: test_payment.py    From ecommerce with GNU Affero General Public License v3.0 5 votes vote down vote up
def add_item_to_basket(self, selenium, sku):
        page_css_selector = ".page__payment"

        # Add the item to the basket and start the checkout process
        selenium.get(EcommerceHelpers.build_url(
            '/basket/add/?sku={}'.format(
                sku,
            )
        ))

        # Wait till the selector is visible
        WebDriverWait(selenium, 20).until(
            EC.visibility_of_element_located((By.CSS_SELECTOR, page_css_selector))
        ) 
Example #12
Source File: Autoticket.py    From Autoticket with MIT License 5 votes vote down vote up
def check_order_1(self):
        if self.status in [3, 4]:
            print('###开始确认订单###')
            button_xpath = " //*[@id=\"confirmOrder_1\"]/div[%d]/button" # 同意以上协议并提交订单Xpath
            button_replace = 8 # 当实名者信息不空时为9,空时为8
            if self.real_name: # 实名者信息不为空
                button_replace = 9
                print('###选择购票人信息###')
                try:
                    list_xpath = "//*[@id=\"confirmOrder_1\"]/div[2]/div[2]/div[1]/div[%d]/label/span[1]/input"
                    for i in range(len(self.real_name)): # 选择第i个实名者
                        WebDriverWait(self.driver, self.total_wait_time, self.refresh_wait_time).until(
                            EC.presence_of_element_located((By.XPATH, list_xpath%(i+1)))).click()
                except Exception as e:
                    print(e)
                    raise Exception("***错误:实名信息框未显示,请检查网络或配置文件***")
            submitbtn = WebDriverWait(self.driver, self.total_wait_time, self.refresh_wait_time).until(
                    EC.presence_of_element_located(
                        (By.XPATH, button_xpath%button_replace))) # 同意以上协议并提交订单
            submitbtn.click()  
            '''# 以下的方法更通用,但是更慢
            try:
                buttons = self.driver.find_elements_by_tag_name('button') # 找出所有该页面的button
                for button in buttons:
                    if button.text == '同意以上协议并提交订单':
                        button.click()
                        break
            except Exception as e:
                raise Exception('***错误:没有找到提交订单按钮***')
           '''
            try:
                WebDriverWait(self.driver, self.total_wait_time, self.refresh_wait_time).until(
                    EC.title_contains('支付宝'))
                self.status = 6
                print('###成功提交订单,请手动支付###')
                self.time_end = time()
            except Exception as e:
                print('---提交订单失败,请查看问题---')
                print(e) 
Example #13
Source File: Autoticket.py    From Autoticket with MIT License 5 votes vote down vote up
def check_order_2(self):
        if self.status in [3, 4]:
            print('###开始确认订单###')
            if self.real_name: # 实名者信息不为空
                print('###选择购票人信息###')
                try:
                    tb = WebDriverWait(self.driver, self.total_wait_time, self.refresh_wait_time).until(
                        EC.presence_of_element_located(
                        (By.CLASS_NAME, 'from-1')))
                    tb.find_element_by_tag_name('a').click() # 点击选择购票人按钮
                    
                    sleep(self.intersect_wait_time)
                    # 此处好像定位不到实名者框,还没有解决
                    lb_list = WebDriverWait(self.driver, self.total_wait_time, self.refresh_wait_time).until(
                        EC.presence_of_element_located(
                        (By.XPATH, '/html/body/div[3]/div[3]/div[12]/div/div[2]/div/div[2]/div/table/tbody'))) # 定位弹窗
                    lb = lb_list.find_elements_by_tag_name('input')
                    for i in range(len(self.real_name)):
                        lb[self.real_name[i] - 1].find_element_by_tag_name('input').click()  # 选择第self.real_name个实名者
                except Exception as e:
                    print(e)
            input('halt')
            WebDriverWait(self.driver, self.total_wait_time, self.refresh_wait_time).until(
                        EC.presence_of_element_located(
                        (By.ID, 'orderConfirmSubmit'))).click() # 同意以上协议并提交订单
            # self.driver.find_element_by_id('orderConfirmSubmit').click() 
            element = WebDriverWait(self.driver, 10, self.refresh_wait_time).until(EC.title_contains('选择支付方式'))
            element.find_element_by_xpath('/html/body/div[5]/div/div/div/ul/li[2]/a').click()  # 默认选择支付宝
            element.find_element_by_xpath('/html/body/div[5]/div/div/form/div[2]/ul/li[1]/label/input').click()
            element.find_element_by_id('submit2').click()  # 确认无误,支付
            self.status = 6
            print('###成功提交订单,请手动支付###')
            self.time_end = time()
            # print('###提交订单失败,请查看问题###') # 这里异常处理还有点问题 
Example #14
Source File: common.py    From pixelated-user-agent with GNU Affero General Public License v3.0 5 votes vote down vote up
def wait_until_element_is_invisible_by_locator(context, locator_tuple, timeout=TIMEOUT_IN_S):
    wait = WebDriverWait(context.browser, timeout)
    wait.until(EC.invisibility_of_element_located(locator_tuple)) 
Example #15
Source File: common.py    From pixelated-user-agent with GNU Affero General Public License v3.0 5 votes vote down vote up
def _wait_until_element_is_visible_by_locator(context, locator_tuple, timeout=TIMEOUT_IN_S):
    wait = WebDriverWait(context.browser, timeout)
    wait.until(EC.visibility_of_element_located(locator_tuple))
    return context.browser.find_element(locator_tuple[0], locator_tuple[1]) 
Example #16
Source File: common.py    From pixelated-user-agent with GNU Affero General Public License v3.0 5 votes vote down vote up
def wait_for_condition(context, predicate_func, timeout=TIMEOUT_IN_S, poll_frequency=0.1):
    wait = WebDriverWait(context.browser, timeout, poll_frequency=poll_frequency)
    wait.until(predicate_func) 
Example #17
Source File: common.py    From pixelated-user-agent with GNU Affero General Public License v3.0 5 votes vote down vote up
def wait_until_button_is_visible(context, title, timeout=TIMEOUT_IN_S):
    wait = WebDriverWait(context.browser, timeout)
    locator_tuple = (By.XPATH, ("//%s[contains(.,'%s')]" % ('button', title)))
    wait.until(EC.visibility_of_element_located(locator_tuple)) 
Example #18
Source File: base.py    From FoxPuppet with Mozilla Public License 2.0 5 votes vote down vote up
def __init__(self, selenium, handle):
        """Create a BaseWindow object.

        Args:
            selenium:
                (:py:class:`~selenium.webdriver.remote.webdriver.WebDriver`):
                Firefox WebDriver object.
            handle: (str): WebDriver Firefox window handle.
        """
        self.selenium = selenium
        self.handle = handle
        self.wait = WebDriverWait(self.selenium, timeout=10) 
Example #19
Source File: actions.py    From linkedin_scraper with GNU General Public License v3.0 5 votes vote down vote up
def login(driver, email=None, password=None):
  if not email or not password:
    email, password = __prompt_email_password()

  driver.get("https://www.linkedin.com/login")
  element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "username")))

  email_elem = driver.find_element_by_id("username")
  email_elem.send_keys(email)

  password_elem = driver.find_element_by_id("password")
  password_elem.send_keys(password)
  driver.find_element_by_tag_name("button").click()

  element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "profile-nav-item"))) 
Example #20
Source File: reader.py    From Lector with MIT License 5 votes vote down vote up
def _wait(self, timeout=10):
    """Returns a `WebDriverWait` instance set to `timeout` seconds."""

    return WebDriverWait(self, timeout=timeout) 
Example #21
Source File: util.py    From micromasters with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def wait(self, timeout=15):
        """Helper method to instantiate a WebDriverWait"""
        return WebDriverWait(driver=self.driver, timeout=timeout) 
Example #22
Source File: automation.py    From pybitrix24 with MIT License 5 votes vote down vote up
def __init__(self, bitrix24, headless=True):
        self.bx24 = bitrix24
        self.headless = headless
        self.driver = Chrome(options=self._provide_chrome_options())
        self.wait = WebDriverWait(self.driver, 60) 
Example #23
Source File: basewebobject.py    From avos with Apache License 2.0 5 votes vote down vote up
def _wait_till_text_present_in_element(self, locator, text):
        condition = expected_conditions.text_to_be_present_in_element(locator,
                                                                      text)
        try:
            wait.WebDriverWait(self.driver, self.explicit_wait).\
                until(condition)
        except Exceptions.TimeoutException:
            return False
        return True 
Example #24
Source File: core_test.py    From tensorboard with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        self.driver = webtest.new_webdriver_session()
        self.driver.get("http://localhost:%s" % self.port)
        self.wait = wait.WebDriverWait(self.driver, 10) 
Example #25
Source File: plugin.py    From pytest-splinter with MIT License 5 votes vote down vote up
def _wait_for_condition(self, condition=None, timeout=None, poll_frequency=0.5, ignored_exceptions=None):
    """Wait for given javascript condition."""
    condition = functools.partial(condition or self.visit_condition, self)

    timeout = timeout or self.wait_time

    return wait.WebDriverWait(
        self.driver, timeout, poll_frequency=poll_frequency, ignored_exceptions=ignored_exceptions
    ).until(
        lambda browser: condition()
    ) 
Example #26
Source File: utils.py    From debbit with MIT License 5 votes vote down vote up
def is_logged_in(driver, timeout=30, logged_out_element=None, logged_in_element=None):
    login_status = WebDriverWait(driver, timeout).until(
        lambda driver:
        (driver.find_elements(*logged_out_element) and 'logged_out')
        or
        (driver.find_elements(*logged_in_element) and 'logged_in')
    )

    if login_status == 'logged_out':  # TODO is there any way we know this is a use_cookies:yes secondary purchase?
        LOGGER.info('login_status=logged_out, logging in now')
        return False
    else:
        LOGGER.info('login_status=logged_in')
        return True


# Useful with WebDriverWait() and multiple expected conditions.
# This function finishes the moment any condition returns true.
# Example usage:
#
# try:
#     WebDriverWait(driver, 30).until(utils.AnyExpectedCondition(
#         expected_conditions.element_to_be_clickable((By.ID, 'some-element-id')),
#         expected_conditions.element_to_be_clickable((By.XPATH, "//*[contains(text(),'some text on webpage')]")),
#         expected_conditions.element_to_be_clickable((By.XPATH, "//img[contains(@src,'someDescription')]"))
#     ))
# except TimeoutException:
#     LOGGER.info('Timed out looking for expected conditions after 30 seconds') 
Example #27
Source File: base.py    From django-bootstrap-modal-forms with MIT License 5 votes vote down vote up
def wait_for(self, class_name=None, element_id=None, tag=None, xpath=None):
        return WebDriverWait(self.browser, 20).until(
            expected_conditions.element_to_be_clickable
            ((By.ID, element_id) if element_id else
             (By.CLASS_NAME, class_name) if class_name else
             (By.TAG_NAME, tag) if tag else
             (By.XPATH, xpath))
        ) 
Example #28
Source File: base.py    From appstore with GNU Affero General Public License v3.0 5 votes vote down vote up
def wait_for_url(self, url: str) -> Any:
        WebDriverWait(self.selenium, SELENIUM_WAIT_SEC).until(
            EC.url_contains(url)
        ) 
Example #29
Source File: base.py    From appstore with GNU Affero General Public License v3.0 5 votes vote down vote up
def wait_for(self, selector: str,
                 then: Callable[[WebElement], None]) -> Any:
        element = WebDriverWait(self.selenium, SELENIUM_WAIT_SEC).until(
            EC.visibility_of_element_located((By.CSS_SELECTOR, selector)))
        return then(element) 
Example #30
Source File: xuexi.py    From autoxuexi with GNU General Public License v3.0 5 votes vote down vote up
def get_score(self):
        login_url = r'https://pc.xuexi.cn/points/login.html.*'  # check if it has been login
        # score_url = r'https://pc.xuexi.cn/points/my-points.html.*'
        # if not re.match(login_url, self.driver.current_url) and not re.match(score_url, self.driver.current_url):
        #     self.driver.get('https://pc.xuexi.cn/points/my-points.html')
        # self.driver.switch_to.default_content()

        score_url = 'https://pc.xuexi.cn/points/my-points.html'

        try:
            self.driver.get(score_url)
        except selenium.common.exceptions.NoSuchWindowException as error:
            raise Exception(error)

        if re.match(login_url, self.driver.current_url):
            if not self.login():
                return []
        else:
            try:
                WebDriverWait(self.driver, 3).until(
                    expected_conditions.presence_of_element_located((By.XPATH, '//div[@class="my-points-card-text"]')))
            except Exception:
                return []

        score = []
        app.log(u'当前得分情况:')
        score_title = iter([u'每日登陆', u'阅读文章', u'观看视频', u'文章学习时长', u'视频学习时长'])
        for s in self.driver.find_elements_by_xpath('//div[@class="my-points-card-text"]'):
            app.log(u'%s: %s' % (next(score_title), s.text), printtime=False)
            try:
                score.append({'score': int(s.text.split('/')[0][:-1]), 'target': int(s.text.split('/')[1][:-1])})
            except Exception:
                pass

        return score