• python


    # -*- coding:utf-8 -*-
    
    '''
    @project: web学习
    @author: Jimmy
    @file: wait.py
    @ide: PyCharm Community Edition
    @time: 2019-01-19 09:50
    @blog: https://www.cnblogs.com/gotesting/
    
    '''
    
    
    from selenium import webdriver
    import time
    
    driver = webdriver.Chrome()
    driver.get('http://www.baidu.com')
    driver.find_element_by_xpath('//div[@id="u1"]//a[@name="tj_login"]').click()
    
    
    
    # 1. 强制等待
    time.sleep(2)
    
    # 2. 隐性等待
    # 设置最长等待时间,在这个时间段内只要有个时间点加载完成,则执行下一步代码,比sleep智能,能够自主判断
    # 整个driver的会话周期内,设置一次即可
    driver.implicitly_wait(3)
    
    # 3. 显性等待
    # 明确等到某个条件满足后,再去执行下一步操作
    # 程序每隔XX秒看一眼,如果条件成立了,则执行下一步;否则继续等待,知道超过设置的最长时间,然后抛出TimeoutException
    # (1)WebDriverWait(driver,等待时长,轮循周期).until() / until_not()
    # (2)expected_conditions模块,提供了一系列期望发生的条件
    
    # A.  使用之前,引入相关的库:
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import  expected_conditions as EC
    from selenium.webdriver.common.by import By
    # B.  先确定元素的定位表达式
    
    ele_locator = "TANGRAM__PSP_10__footerULoginBtn"
    
    # C.  使用expected_conditions对应的方法来生成判断条件
    # EC.方法名(定位方式,定位表达式)
    # EC.visibility_of_element_located(By.ID,ele_locator)
    
    # D.  调用WebDriverWait类设置等待总时长、轮询周期
    # 等待10秒钟,每隔1秒去查看对应的元素是否可见;如果可见,继续下一步操作;如果不可见,则继续等待,直到10s结束,如果元素还是不可见,则抛出超时异常
    WebDriverWait(driver,10,1).until(EC.visibility_of_element_located((By.ID,ele_locator)))
    
    
    driver.find_element_by_id('TANGRAM__PSP_10__footerULoginBtn').click()
    
    driver.close()
  • 相关阅读:
    【spring mvc】application context中【bean】的生命周期
    【spring mvc】基础概念
    TSql Work with comma
    统计公司人数
    t_sql中的COUNT函数
    T_SQL又另外两种找出3年连续获奖的人
    A Sql Stumper
    验证ISIN, SEDOLE和CUSIP
    按月份进行统计
    使用4中不同的方式找出连续三年获奖的人
  • 原文地址:https://www.cnblogs.com/gotesting/p/10290720.html
Copyright © 2020-2023  润新知