zoukankan      html  css  js  c++  java
  • selenium webdriver python 等待

    AJAX,即“Asynchronous Javascript And  XML”。可以实现网页的异步更新。也就是在不重新加载整个网页的情况下,对网页的某部分进行更新。
    现在大多数网站都使用AJAX技术,这使得网页加载完成之后有些元素还没有出现。为了解决这个问题,保证脚本运行的稳定性,selenium使用了等待功能。
    Selenium的等待分为两种:显示等待、隐式等待。

    添加休眠

     

    import time

    time.sleep(0.3)

    隐式等待

     
    设置一次即可,作用于WebDriver的整个生命周期。
    元素如果找不到都会默认等待30s,直到找到该元素。也就是查找元素时会有一个30s的等待buffer。
    driver.implicitly_wait(30)
     

    显式等待

     
    WebDriverWait(driver,wait_time).until(contion)
     
    显示等待会让WebDriver保持等待,直到特定的条件出现。
    显式等待默认会每隔500毫秒扫描一次页面变化直到特定条件满足或者wait_time用尽。
    在wait_time时间内,如果找到元素则返回该元素,否则raise TimeoutException。
     
    #10s内每隔500毫秒扫描一次页面变化,找到id 为myDynamicElement的元素
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "myDynamicElement")))

    expected_conditions的常用方法:

    title_is
    title_contains
    presence_of_element_located
    visibility_of_element_located
    visibility_of
    presence_of_all_elements_located
    text_to_be_present_in_element
    text_to_be_present_in_element_value
    frame_to_be_available_and_switch_to_it
    invisibility_of_element_located
    element_to_be_clickable - it is Displayed and Enabled.
    staleness_of
    element_to_be_selected
    element_located_to_be_selected
    element_selection_state_to_be
    element_located_selection_state_to_be
    alert_is_present
     
  • 相关阅读:
    Nodejs-内置核心模块&npm包管理工具

    python 内置函数
    序列化和反序列化(json 和pickle)dumps 为序列化, json为反序列化
    迭代器iter()
    生成器 yield
    装饰器的解释说明
    面向过程中的局部变量(global)
    函数的参数设定
    集合的基本操作
  • 原文地址:https://www.cnblogs.com/miniren/p/4980207.html
Copyright © 2011-2022 走看看