您的位置: 网站首页> selenium爬虫> 当前文章

强制等待、显式等待、隐式等待代码示例

老董-我爱我家房产SEO2020-07-28187围观,149赞

  场景

  1.打开百度,等待输入框元素和搜搜按钮出现(输入框和搜索按钮出现才说明网页加载好了)

  2.输入www.pythjon66.com,点击搜索

  3.在搜索结果页等待下一页这个元素出现(出现下一页的话,说明网页加载到了底部,页面的数据比较全了)

  一:强制等待

	# -*- coding: utf-8 -*-

import time
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
from selenium.webdriver.chrome.options import Options


def get_html(kwd, retry=2):
    try:
        driver.get('https://www.baidu.com/')
        # 强制等待5秒尽量确保页面加载好
        time.sleep(5)
        # 输入框
        input = driver.find_element_by_id('kw')
        input.click()  # 先click后clear,直接send_keys容易丢失字符
        input.clear()
        # 输入文字
        for wd in kwd:
            input.send_keys(wd)
        # 搜索按钮
        baidu = driver.find_element_by_id('su')
        # 点击搜索
        baidu.click()
        # 强制等待5秒尽量让搜索结果页加载完
        time.sleep(5)
    except Exception as e:
        print(e)
        time.sleep(10)
        if retry > 0:
            get_html(kwd, retry - 1)
    else:
        title = driver.title
    return title


if __name__ == "__main__":

    pc_ua = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36'
    option = Options()
    prefs = {
        'profile.default_content_setting_values': {
            'images' : 2, # 禁止图片加载
            'notifications': 2  # 禁止弹窗
        }
    }
    option.add_experimental_option('prefs', prefs)
    driver = webdriver.Chrome(options=option)
    title = get_html('www.python66.com')
    print(title)
    driver.quit()

  二:显式等待

# -*- coding: utf-8 -*-

import time
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
from selenium.webdriver.chrome.options import Options


def get_html(kwd, retry=2):
    try:
        driver.get('https://www.baidu.com/')
        # 输入框元素,显式等待设定最多5秒
        input = WebDriverWait(driver, 5).until(
            EC.visibility_of_element_located((By.ID, "kw"))
        )
        input.click()  # 先click后clear,直接send_keys容易丢失字符
        input.clear()
        # 输入文字
        for wd in kwd:
            input.send_keys(wd)
        # 搜索按钮元素,显式等待设定最多5秒
        baidu = WebDriverWait(driver, 5).until(
            EC.visibility_of_element_located((By.ID, "su"))
        )
        # 点击搜索
        baidu.click()
        # 下一页,显式等待设定最多5秒(有的词没有相关搜索可能报错)
        xg_serach = WebDriverWait(driver, 15).until(
            EC.visibility_of_element_located((By.CLASS_NAME, "tt"))
        )
    except Exception as e:
        print(e)
        time.sleep(10)
        if retry > 0:
            get_html(kwd, retry - 1)
    else:
        title = driver.title
    return title


if __name__ == "__main__":

    pc_ua = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36'
    option = Options()
    prefs = {
        'profile.default_content_setting_values': {
            'images' : 2, # 禁止图片加载
            'notifications': 2  # 禁止弹窗
        }
    }
    option.add_experimental_option('prefs', prefs)
    driver = webdriver.Chrome(options=option)
    title = get_html('www.python66.com')
    print(title)
    driver.quit()

  三、隐式等待

# -*- coding: utf-8 -*-


import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options


def get_html(kwd, retry=2):
    try:
        driver.get('https://www.baidu.com/')
        # 输入框
        input = driver.find_element_by_id('kw')
        input.click()  # 先click后clear,直接send_keys容易丢失字符
        input.clear()
        # 输入文字
        for wd in kwd:
            input.send_keys(wd)
        print(driver.window_handles,driver.current_url)
        # 搜索按钮
        baidu = driver.find_element_by_id('su')
        # 点击搜索
        baidu.click()
        # 底部文本 此处也会隐式等待
        element = driver.find_element_by_id('help')
    except Exception as e:
        print(e)
        time.sleep(10)
        if retry > 0:
            get_html(kwd, retry - 1)
    else:
        print(driver.window_handles,driver.current_url)
        title = driver.title
    return title


if __name__ == "__main__":

    pc_ua ='Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36'
    option = Options()
    prefs = {
       'profile.default_content_setting_values': {
           'images' : 2, # 禁止图片加载
           'notifications': 2  # 禁止弹窗
        }
    }
    option.add_experimental_option('prefs', prefs)
    driver = webdriver.Chrome(options=option)
    # 隐式等待设置5秒
    driver.implicitly_wait(30)
    title = get_html('python')
    print(title)


if __name__ == "__main__":

    pc_ua = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36'
    option = Options()
    prefs = {
        'profile.default_content_setting_values': {
            'images' : 2, # 禁止图片加载
            'notifications': 2  # 禁止弹窗
        }
    }
    option.add_experimental_option('prefs', prefs)
    driver = webdriver.Chrome(options=option)
    # 隐式等待设置5秒
    driver.implicitly_wait(5)
    title = get_html('www.python66.com')
    print(title)
    # driver.quit() #如果设置了driver.quit()会很快退出,网页的title还是百度首页的title

['CDwindow-0C09496C4890920231EFAB157730936A'] https://www.baidu.com/
['CDwindow-0C09496C4890920231EFAB157730936A'] https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=python&rsv_t=8a68lZY5YfYYb3CvaeMfYM9TJANA52kQOO484WvXZOLSo7BSB5x0t1xvfgE&rsv_enter=0&rsv_dl=tb&rsv_sug3=7&rsv_sug1=1&rsv_sug7=100&inputT=270&rsv_sug4=325
python_百度搜索


很赞哦!

python编程网提示:转载请注明来源www.python66.com。
有宝贵意见可添加站长微信(底部),获取技术资料请到公众号(底部)。同行交流请加群 python学习会

文章评论

    强制等待、显式等待、隐式等待代码示例文章写得不错,值得赞赏

站点信息

  • 网站程序:Laravel
  • 客服微信:a772483200