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
     
  • 相关阅读:
    12.2 ROS NavFn全局规划源码解读_2
    12.1 ROS NavFn全局规划源码_1
    12 ROS Movebase主体源码解读
    11 ROS 动态参数调节
    10. ROS costmap代价地图
    无人驾驶汽车1: 基于Frenet优化轨迹的无人车动作规划方法
    VC下加载JPG/GIF/PNG图片的两种方法
    vc++加载透明png图片方法-GDI+和CImage两种
    供CImage类显示的半透明PNG文件处理方法
    使用MFC CImage类绘制PNG图片时遇到的问题
  • 原文地址:https://www.cnblogs.com/miniren/p/4980207.html
Copyright © 2011-2022 走看看