zoukankan      html  css  js  c++  java
  • 【python爬虫】selenium的三种等待

    一、 强制等待

      第一种也是最简单粗暴的一种办法就是强制等待sleep(xx),强制让闪电侠等xx时间,不管凹凸曼能不能跟上速度,还是已经提前到了,都必须等xx时间。

    1 from selenium import webdriver
    2 from time import sleep
    3 driver = webdriver.Firefox()
    4 driver.get('https://huilansame.github.io')
    5 sleep(3) # 强制等待3秒再执行下一步
    6 print driver.current_url
    7 driver.quit()

      这种叫强制等待,不管你浏览器是否加载完了,程序都得等待3秒,3秒一到,继续执行下面的代码,作为调试很有用,有时候也可以在代码里这样等待,不过不建议总用这种等待方式,太死板,严重影响程序执行速度。

    二、隐性等待

    1 from selenium import webdriver
    2 driver = webdriver.Firefox()
    3 driver.implicitly_wait(30) # 隐性等待,最长等30秒
    4 driver.get('https://huilansame.github.io')
    5 print driver.current_url
    6 driver.quit()

      隐形等待是设置了一个最长等待时间,如果在规定时间内网页加载完成,则执行下一步,否则一直等到时间截止,然后执行下一步。

      注意这里有一个弊端,那就是程序会一直等待整个页面加载完成,也就是一般情况下你看到浏览器标签栏那个小圈不再转,才会执行下一步,但有时候页面想要的元素早就在加载完成了,但是因为个别js之类的东西特别慢,我仍得等到页面全部完成才能执行下一步,我想等我要的元素出来之后就下一步怎么办?有办法,这就要看selenium提供的另一种等待方式——显性等待wait了。

    三、 显性等待

      第三种办法就是显性等待,WebDriverWait,配合该类的until()和until_not()方法,就能够根据判断条件而进行灵活地等待了。它主要的意思就是:程序每隔xx秒看一眼,如果条件成立了,则执行下一步,否则继续等待,直到超过设置的最长时间,然后抛出TimeoutException。

    from selenium import webdriver
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    driver = webdriver.Firefox()
     1 # 隐性等待和显性等待可以同时用,但要注意:等待的最长时间取两者之中的大者
     2 driver.implicitly_wait(10)
     3  
     4 driver.get('https://huilansame.github.io')
     5 locator = (By.LINK_TEXT, 'CSDN')
     6 try:
     7     wait = WebDriverWait(driver, 20, 0.5)
     8     wait.until(EC.presence_of_element_located(locator))
     9     print(driver.find_element_by_link_text('CSDN').get_attribute('href'))
    10 finally:
    11 driver.close()

    expected_conditions模块

      expected_conditions是selenium的一个模块,其中包含一系列可用于判断的条件。

    1 EC.title_is
    2 EC.title_contains
    3 # 这两个条件类验证title,验证传入的参数title是否等于或在driver.title中 
    1 EC.presence_of_element_located((By.CSS_SELECTOR,'.ui-page > wrap'))
    2 EC.presence_of_all_elements_located((By.CSS_SELECTOR,'.ui-page'))
    3 # 这两个条件验证元素是否出现,传入的参数都是元组类型的locator,如(By.ID, 'kw')
    4 # 一个只要一个符合条件的元素加载出来就通过;
    5 # 另一个必须所有符合条件的元素都加载出来才行
    1 EC.visibility_of_element_located
    2 EC.invisibility_of_element_located
    3 EC.visibility_of
    4 # 这三个条件验证元素是否可见
    5 # 前两个传入参数是元组类型的locator,第三个传入WebElement
    6 # 第一个和第三个其实质是一样的
    1 EC.text_to_be_present_in_element
    2 EC.text_to_be_present_in_element_value
    3 # 这两个判断某段文本是否出现在某元素中
    4 # 一个判断元素的text,一个判断元素的value属性
    1 EC.frame_to_be_available_and_switch_to_it
    2 # 这个条件判断frame是否可切入,
    3 # 可传入locator元组或者直接传入定位方式:id、name、index或WebElement 
     1 # 这个条件判断是否有alert出现
     2 EC.alert_is_present
     3 #这个条件判断元素是否可点击,传入locator
     4 EC.element_to_be_clickable
     5 # 这四个条件判断元素是否被选中,
     6 第一个条件传入WebElement对象,第二个传入locator元组
     7 # 第三个传入WebElement对象以及状态,相等返回True,否则返回False
     8 # 第四个传入locator以及状态,相等返回True,否则返回False
     9 EC.element_to_be_selected
    10 EC.element_located_to_be_selected
    11 EC.element_selection_state_to_be
    12 EC.element_located_selection_state_to_be
    13 # 最后一个条件判断一个元素是否仍在页面中,传入WebElement对象,可以判断页面是否刷新
    14 EC.staleness_of
     
  • 相关阅读:
    Java+7入门经典 -1 简介
    优化算法动画演示Alec Radford's animations for optimization algorithms
    如何写科技论文How to write a technical paper
    开始学习深度学习和循环神经网络Some starting points for deep learning and RNNs
    用500行Julia代码开始深度学习之旅 Beginning deep learning with 500 lines of Julia
    用10张图来看机器学习Machine learning in 10 pictures
    ICLR 2013 International Conference on Learning Representations深度学习论文papers
    ICLR 2014 International Conference on Learning Representations深度学习论文papers
    卷积神经网络CNN(Convolutional Neural Networks)没有原理只有实现
    卷积神经网络Convolutional Neural Networks
  • 原文地址:https://www.cnblogs.com/Tree0108/p/12089510.html
Copyright © 2011-2022 走看看