• selenium 等待


    0.安装selenium + Chrome Driver
    安装selenium:
    pip install selenium
    安装Chrome Driver:

    下载:http://chromedriver.storage.googleapis.com/index.html
    版本要对应(chrome://version查看版本)
    将chromedriver.exe 添加到用户环境变量
    1.使用selenium模拟浏览器操作demo
    from selenium import webdriver
    import time

    # 0.创建浏览器对象
    browser = webdriver.Chrome()

    # 1.访问页面
    browser.get('http://www.baidu.com')

    print(browser.title) # 百度一下,你就知道

    # print(type(browser.find_element_by_class_name("s-top-left"))) # <class 'selenium.webdriver.remote.webelement.WebElement'>

    # 2.定位网页元素,可使用( Selenium自带API || 通过browser.page_source获取网页源码再结合BeautifulSoup等解析工具定位元素)

    browser.find_element_by_link_text("新闻").click() # 模拟鼠标点击文本为“新闻”的链接

    print(browser.current_url) # https://www.baidu.com/

    print(browser.page_source) # 获取网页源码

    time.sleep(5)
    browser.quit() # 浏览器关闭
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    效果:打开Chrome,点击新闻,等待5s,浏览器关闭

    2.selenium模拟在豆瓣中搜索框中搜索指定关键词
    from selenium import webdriver
    import time

    browser = webdriver.Chrome()
    browser.get('http://douban.com')
    time.sleep(1)

    search_box = browser.find_element_by_name('q') # 搜索框
    search_box.send_keys('Python')

    bt1 = browser.find_element_by_class_name('bn') #提交按钮
    bt1.click()

    time.sleep(5)
    browser.quit()
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    3.selenium模拟页面下拉滚动
    主要有两种方式:

    模拟键盘输入(如输入PageDown)
    from selenium import webdriver
    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.keys import Keys
    import time

    browser = webdriver.Chrome()
    browser.get('http://news.baidu.com/')

    for i in range(20):
    # 模拟键盘输入方式向下滚动,perform()执行ActionChains存储的所有动作
    # 链式模型:ActionChains(browser).send_keys(x).click(y).move_to_element(z).perform()
    # 相当于执行三个动作( t = ActionChains(browser) , t.send_keys(x) , t.click(y)
    # t.move_to_element(z) , t.perform()
    ActionChains(browser).send_keys(Keys.PAGE_DOWN).perform()

    time.sleep(0.5)

    browser.quit()
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    执行JavaScript代码
    from selenium import webdriver
    import time

    browser = webdriver.Chrome()
    browser.get('http://news.baidu.com/')

    for i in range(10):
    # scrollTo(x,y)方法:将内容滚动到指定坐标
    browser.execute_script("window.scrollTo(0,document.body.scrollHeight)") # 滚动到页面底部
    time.sleep(0.5)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    4.selenium等待机制
    使用Selenium时也要注意等待机制,以保证浏览器被驱动时能够有寻找元素的缓冲时间。
    分为隐式等待,显式等待

    隐式等待可以直接通过浏览器驱动对象implicitly_wait(x)调用(不灵活、写法简单
    browser = webdriver.Chrome()

    browser.implicitly_wait(x) # 若下面的find_element_by_id()未能立即获得结果,则保持轮询并等待x秒
    browser.get('a url')
    browser.find_element_by_id('id_name')
    1
    2
    3
    4
    5
    显式等待通过结合WebDriverWait 和 Expected Condition使用:(等待某一条件发生,较灵活
    from selenium import webdriver
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By

    browser = webdriver.Chrome()
    browser.get('a url')

    WebDriverWait(browser,10,0.5).until(EC.presence_of_element_located((By.LINK_TEXT, 'text'))).click()

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    其中,
    WebDriverWait(driver=browser, timeout=10, poll_frequency=0.5, ignored_exceptions=None):

    driver:浏览器驱动
    timeout:最长超时时间,单位(秒)
    poll_frequency:轮询检测时间,也就是每隔多少时间检测一次,默认是0.5秒
    ignored_exceptions:超时后的异常信息,默认抛出NoSuchElementException

  • 相关阅读:
    CSS盒子模型
    Git
    FFmpeg
    根据经纬度查询当前地点,百度google接口
    取数组中随机元素
    PHP获取当前页面url
    冒泡
    PHP5中使用PDO连接数据库的方法
    js循环遍历
    判断网页中英文
  • 原文地址:https://www.cnblogs.com/pythonClub/p/16126882.html
Copyright © 2020-2023  润新知