zoukankan      html  css  js  c++  java
  • selenium三个等待时间

    1.强制等待sleep(秒):当设置时间很少,1秒2秒的设置可以用sleep,长时间等待不适用。

    import time
    
    time.sleep(10) #10单位是s

    2.隐性等待:设置最长等待时间,在这个时间内加载完成,则执行下一步。整个driver的会话周期内,设置一次即可,全局都可用。--比如:设置最长等待30s,在查找到元素时,即进行下一步操作。若超时仍未找到,则报错,提示未找到元素

    from selenium import webdriver
    
    driver = webdriver.Chrome()
    
    driver.implicitly_wait(30)

    注:在设置隐性等待中再设置强制等待,会生效

    3.显性等待(重点):明确等到某个条件满足之后,再去执行下一步操作。

       程序每隔xx秒看一眼,如果条件成立了,则执行下一步,否则继续等待,直到超过设定的最长时间,然后抛出TimeoutException。

       WebDriverWait类:显性等待类

       WebDriverWait(driver,等待时长,轮循周期).until/until_not

       等待时长单位:秒

    1)引入三个包:By类中提供了8种定位方式。EC中提供了很多期望条件的类。

    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By

    2)找到元素存在,并传入参数类型与值,元组类型。其中10为timeout时间。

    ele_locator = "//a[text()='用法详解 - 道高一尺 - 博客园']"
    param = (By.XPATH,ele_locator)
    #使用webdriverwait,使得操作的元素存在
    WebDriverWait(driver,10).until(EC.presence_of_element_located(param))

    3)Expected_conditions模块:提供了一系列期望发生的条件

    a.title_is:

    b.title_contains:

    c.presence_of_element_located:元素存在

    d.presence_of_all_elements_located:所有元素存在

    e.url_matches:

    f.url_to_be:

    g.url_changes:

    h.url_contains:

    i.visibility_of

    j.visibility_of_element_located:元素可见

    k.invisibility_of_element_located

    l.text_to_be_present_in_element_value

    m.text_to_be_present_in_element

    n.element_to_be_clickable:元素可点击

    o.staleness_of

    p.element_to_be_selected

    q.element_located_to_be_selected

    r.element_selection_state_to_be

    s.number_of_windows_to_be

    t.alert_is_present:alert元素存在

    u.new_window_is_opened:新窗口打开

  • 相关阅读:
    博世传感器调试笔记(一)----加速度传感器BMA253
    博世传感器调试笔记(二)加速度及陀螺仪传感器BMI160
    如何分区硬盘
    " " 与" " 区别
    SPI、I2C、UART、I2S、GPIO、SDIO、CAN
    nandflash,norflash,sdram,emmc,rom,ram等各种存储器识别
    AAC音频格式详解
    【转载】音频基础知识
    指针数组和数组指针的区别
    关于720p和1080p观看距离和效果
  • 原文地址:https://www.cnblogs.com/xiaoxiaolvdou/p/9276548.html
Copyright © 2011-2022 走看看