zoukankan      html  css  js  c++  java
  • 关于如何等待一个元素的出现而不用一些笨拙粗暴的time.sleep()方法

    我相信这是一个非常大众化的需求,我们需要等待某一个元素的出现以此来让我们的脚本进入到下一个Step,这个等待方法最好能够设置超时时间,然后找到后迅速callback。我们也很幸运!如果你仔细看Selenium的API你会发现这么一个东西:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
    from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
     
    ff = webdriver.Firefox()
    ff.get("http://somedomain/url_that_delays_loading")
    try:
    element = WebDriverWait(ff, 10).until(EC.presence_of_element_located((By.ID, "myDynamicElement")))
    finally:
    ff.quit()

    原来,我们的webdriver.support.ui中有一个强大的WebDriverWait可以帮助我们完成这个事情。
    他提供了一个极其实用的until方法,until方法可以传入一个条件型的参数,这个条件型参数来自selenium.webdriver.support中的expected_conditions,里面定义了一些”call“可调用的类,比如我们这里常用的
    presence_of_element_located,意思就是等待某一元素出现(被定位到)

  • 相关阅读:
    centos7下如何使用udev配置asm磁盘
    ORA-29786: SIHA attribute GET failed with error [Attribute 'SPFILE' sts[200]
    安装grid时报INS-40404错误
    clickhouse编译安装
    centos7通过rc.local文件添加自启动服务
    Error in invoking target 'agent nmhs' of makefile
    打补丁(18370031)
    2020 HFCTF
    2020省赛决赛
    2020西湖论剑
  • 原文地址:https://www.cnblogs.com/jiuyigirl/p/7111796.html
Copyright © 2011-2022 走看看