zoukankan      html  css  js  c++  java
  • Selenium EC 与 Wait

    爬虫经常出现错误,多半是没等待控件加载出来。利用selenium虽然方便,但是还是需要控制一下等待时间。

    Selenium有两个常见的模块来控制等待。

    EC(expected_conditions)

    EC作为预期条件,经常与util()和util_not()连用。

    这是EC的16个方法

    #这两个条件类验证title,验证传入的参数title是否等于或在driver.title中
    EC.title_is
    EC.title_contains
    #这两个条件验证元素是否出现,传入的参数都是元组类型的locator,如(By.ID, 'kw')
    #一个只要一个符合条件的元素加载出来就通过;另一个必须所有符合条件的元素都加载出来才行
    EC.presence_of_element_located((By.CSS_SELECTOR,'.ui-page > div.ui-page-wrap'))
    EC.presence_of_all_elements_located((By.CSS_SELECTOR,'.ui-page > div.ui-page-wrap'))
    #这三个条件验证元素是否可见,前两个传入参数是元组类型的locator,第三个传入WebElement
    #第一个和第三个其实质是一样的
    EC.visibility_of_element_located
    EC.invisibility_of_element_located
    EC.visibility_of
    #这两个人条件判断某段文本是否出现在某元素中,一个判断元素的text,一个判断元素的value属性
    EC.text_to_be_present_in_element
    EC.text_to_be_present_in_element_value
    #这个条件判断frame是否可切入,可传入locator元组或者直接传入定位方式:id、name、index或WebElement
    EC.frame_to_be_available_and_switch_to_it
    #这个条件判断是否有alert出现
    EC.alert_is_present
    #这个条件判断元素是否可点击,传入locator
    EC.element_to_be_clickable
    #这四个条件判断元素是否被选中,第一个条件传入WebElement对象,第二个传入locator元组
    #第三个传入WebElement对象以及状态,相等返回True,否则返回False
    #第四个传入locator以及状态,相等返回True,否则返回False
    EC.element_to_be_selected
    EC.element_located_to_be_selected
    EC.element_selection_state_to_be
    EC.element_located_selection_state_to_be
    #最后一个条件判断一个元素是否仍在页面中,传入WebElement对象,可以判断页面是否刷新
    EC.staleness_of
    

    利用条件来设置等待用的多的是presence_of_element_located

    WebDriverWait

    from selenium.webdriver.support.ui import WebDriverWait
    WebDriverWait为显型等待,配合until()和until_not()方法,就能够根据EC而进行灵活地等待了。
    它主要的意思就是:程序每隔xx秒看一眼,如果条件成立了,则执行下一步,
    否则继续等待,直到超过设置的最长时间,然后抛出TimeoutException。

    #每隔0.5查看DOM里中条件是否成立,一般要被try catch包围
    try{
        WebDriverWait(driver, 10, 0.5).until(EC.presence_of_element_located((By.ID, "nc_1_n1z")))
        swipe_button = self.browser.find_element_by_id('nc_1_n1z') #获取滑动拖动控件
    
        #模拟拽托
        action = ActionChains(self.browser) # 实例化一个action对象
        action.click_and_hold(swipe_button).perform() # perform()用来执行ActionChains中存储的行为
        action.reset_actions()
        action.move_by_offset(580, 0).perform() # 移动滑块
    }except TimeoutException as e1:
        print('Timeout: ', e1)
    except Exception as e:
        print('get button failed: ', e)


    implicitly_wait():隐式等待


    当使用了隐式等待执行测试的时候,如果 WebDriver没有在 DOM中找到元素,将继续等待,超出设定时间后则抛出找不到元素的异常
    换句话说,当查找元素或元素并没有立即出现的时候,隐式等待将等待一段时间再查找 DOM,默认的时间是0
    一旦设置了隐式等待,则它存在整个 WebDriver 对象实例的声明周期中,隐式的等到会让一个正常响应的应用的测试变慢,
    它将会在寻找每个元素的时候都进行等待,这样会增加整个测试执行的时间。

    driver.implicitly_wait(30)
    driver.find_element_by_xpath('//*[@class="weibo-login"]').click()

    sleep()

    有时候在控件加载出来都可能会进行sleep,以防止网站认为是爬虫

  • 相关阅读:
    1.27
    1.25
    Representation Learning with Contrastive Predictive Coding
    Learning a Similarity Metric Discriminatively, with Application to Face Verification
    噪声对比估计(负样本采样)
    Certified Adversarial Robustness via Randomized Smoothing
    Certified Robustness to Adversarial Examples with Differential Privacy
    Dynamic Routing Between Capsules
    Defending Adversarial Attacks by Correcting logits
    Visualizing Data using t-SNE
  • 原文地址:https://www.cnblogs.com/triangle959/p/12024363.html
Copyright © 2011-2022 走看看