• selenium用法详解


    selenium 是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Firefox,Safari,Google Chrome,Opera等。这个工具的主要功能包括:测试与浏览器的兼容性——测试你的应用程序看是否能够很好得工作在不同浏览器和操作系统之上。测试系统功能——创建回归测试检验软件功能和用户需求。支持自动录制动作和自动生成 .Net、Java、Perl等不同语言的测试脚本。
    selenium用于爬虫,主要是用来解决javascript渲染的问题
    selenium的安装详见博客:http://blog.csdn.net/qq_29186489/article/details/78581249
    基本框架
    控制chrome浏览器,访问百度,并搜索关键词Python,获取搜索结果

    # -*- coding: utf-8 -*-
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.wait import WebDriverWait
    import time
    browser=webdriver.Chrome()
    try:
    browser.get("https://www.baidu.com")
    input=browser.find_element_by_id("kw")
    input.send_keys("Python")
    input.send_keys(Keys.ENTER)
    wait=WebDriverWait(browser,10)
    wait.until(EC.presence_of_element_located((By.ID,"content_left")))
    print(browser.current_url)
    print(browser.get_cookies())
    print(browser.page_source)
    time.sleep(10)
    finally:
    browser.close()
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    运行结果如下:

    详细用法如下:
    1:声明浏览器对象

    # -*- coding: utf-8 -*-
    from selenium import webdriver
    #声明谷歌、Firefox、Safari等浏览器
    browser=webdriver.Chrome()
    browser=webdriver.Firefox()
    browser=webdriver.Safari()
    browser=webdriver.Edge()
    browser=webdriver.PhantomJS()
    1
    2
    3
    4
    5
    6
    7
    8
    2:访问页面

    #_*_coding: utf-8_*_

    from selenium import webdriver
    browser=webdriver.Chrome()
    browser.get("http://www.taobao.com")
    print(browser.page_source)
    browser.close()
    1
    2
    3
    4
    5
    6
    7
    3:查找单个元素

    #_*_coding: utf-8_*_

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    browser=webdriver.Chrome()
    browser.get("http://www.taobao.com")
    input_first=browser.find_element_by_id("q")
    input_second=browser.find_element_by_css_selector("#q")
    input_third=browser.find_element(By.ID,"q")
    print(input_first,input_second,input_first)
    browser.close()
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    4:查找多个元素

    #_*_coding: utf-8_*_

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    browser=webdriver.Chrome()
    browser.get("http://www.taobao.com")
    lis=browser.find_element_by_css_selector("li")
    lis_c=browser.find_element(By.CSS_SELECTOR,"li")
    print(lis,lis_c)
    browser.close()
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    5:元素的交互操作
    对获取到的元素调用交互方法

    #_*_coding: utf-8_*_
    from selenium import webdriver
    import time
    browser=webdriver.Chrome()
    browser.get("https://www.taobao.com")
    input=browser.find_element_by_id("q")
    input.send_keys("iPhone")
    time.sleep(10)
    input.clear()
    input.send_keys("iPad")
    button=browser.find_element_by_class_name("btn-search")
    button.click()
    time.sleep(10)
    browser.close()
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    6:交互动作
    把动作附加到交互链中

    #_*_coding: utf-8_*_
    from selenium import webdriver
    from selenium.webdriver import ActionChains
    import time
    from selenium.webdriver.common.alert import Alert
    browser=webdriver.Chrome()
    url="http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable"
    browser.get(url)
    #切换到目标元素所在的frame
    browser.switch_to.frame("iframeResult")
    #确定拖拽目标的起点
    source=browser.find_element_by_id("draggable")
    #确定拖拽目标的终点
    target=browser.find_element_by_id("droppable")
    #形成动作链
    actions=ActionChains(browser)
    actions.drag_and_drop(source,target)
    #执行
    actions.perform()
    '''
    1.先用switch_to_alert()方法切换到alert弹出框上
    2.可以用text方法获取弹出的文本 信息
    3.accept()点击确认按钮
    4.dismiss()相当于点右上角x,取消弹出框
    '''
    t=browser.switch_to_alert()
    print(t.text)
    t.accept()
    time.sleep(10)
    browser.close()
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    7:执行javascript
    下面的例子是执行就是,拖拽进度条到底,并弹出提示框

    #_*_coding: utf-8_*_
    from selenium import webdriver
    browser=webdriver.Chrome()
    browser.get("https://www.zhihu.com/explore")
    browser.execute_script("window.scrollTo(0,document.body.scrollHeight)")
    browser.execute_script("alert('To Button')")
    browser.close()
    1
    2
    3
    4
    5
    6
    7
    8:获取元素信息
    获取属性

    # -*- coding: utf-8 -*-
    from selenium import webdriver

    browser=webdriver.Chrome()
    url="https://www.zhihu.com/explore"
    browser.get(url)
    logo=browser.find_element_by_id("zh-top-link-logo")
    print(logo)
    print(logo.get_attribute("class"))
    browser.close()
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    获取文本值

    # -*- coding: utf-8 -*-
    from selenium import webdriver

    browser=webdriver.Chrome()
    url="https://www.zhihu.com/explore"
    browser.get(url)
    logo=browser.find_element_by_id("zh-top-link-logo")
    print(logo)
    print(logo.text)
    browser.close()
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    获取ID、位置、大小和标签名

    # -*- coding: utf-8 -*-
    from selenium import webdriver

    browser=webdriver.Chrome()
    url="https://www.zhihu.com/explore"
    browser.get(url)
    logo=browser.find_element_by_id("zh-top-link-logo")
    print(logo)
    #id
    print(logo.id)
    #位置
    print(logo.location)
    #标签名
    print(logo.tag_name)
    #大小
    print(logo.size)
    browser.close()
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    9:等待
    隐式等待
    当使用了隐式等待执行测试的时候,如果webdriver没有在DOM中找到元素,将继续等待,超过设定的时间后则抛出找不到元素的异常,换句话说,当查找元素或元素并没有立即出现的时候,隐式等待将等待一段时间再查找DOM,默认时间为0.

    # -*- coding: utf-8 -*-
    from selenium import webdriver

    browser=webdriver.Chrome()
    url="https://www.zhihu.com/explore"
    browser.get(url)
    browser.implicitly_wait(10)
    logo=browser.find_element_by_id("zh-top-link-logo")
    print(logo)
    browser.close()
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    显示等待

    # -*- coding: utf-8 -*-
    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

    browser=webdriver.Chrome()
    url="https://www.taobao.com"
    browser.get(url)
    wait=WebDriverWait(browser,10)
    input=wait.until(EC.presence_of_element_located((By.ID,"q")))
    button=wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,".btn-search")))
    print(input,button)
    browser.close()
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    10:浏览器的前进和后退

    # -*- coding: utf-8 -*-
    from selenium import webdriver
    import time

    browser=webdriver.Chrome()
    browser.get("https://www.taobao.com")
    browser.get("https://www.baidu.com")
    browser.get("https://www.python.org")
    browser.back()
    time.sleep(1)
    browser.forward()
    browser.close()
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    11:cookies的处理

    # -*- coding: utf-8 -*-
    from selenium import webdriver
    import time

    browser=webdriver.Chrome()
    browser.get("https://www.zhihu.com/explore")
    print(browser.get_cookies())
    browser.add_cookie({"name":"name","domain":"www.zhihu.com","value":"germey"})
    print(browser.get_cookies())
    browser.delete_all_cookies()
    print(browser.get_cookies())
    browser.close()
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    12:选项卡管理

    # -*- coding: utf-8 -*-
    from selenium import webdriver
    import time

    browser=webdriver.Chrome()
    browser.get("https://www.zhihu.com/explore")
    browser.execute_script("window.open()")
    print(browser.window_handles)
    browser.switch_to_window(browser.window_handles[1])
    browser.get("https://www.taobao.com")
    time.sleep(1)
    browser.switch_to_window(browser.window_handles[0])
    browser.get("https://python.org")
    browser.close()
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    13:异常处理

    # -*- coding: utf-8 -*-
    from selenium import webdriver
    from selenium.common.exceptions import TimeoutException,NoSuchElementException

    browser=webdriver.Chrome()
    try:
    browser.get("https://www.zhihu.com/explore")
    except TimeoutException:
    print("Time out")
    try:
    browser.find_element_by_id("hello")
    except NoSuchElementException:
    print("No Element")
    finally:
    browser.close()
    ---------------------
    作者:天涯笨熊
    来源:CSDN
    原文:https://blog.csdn.net/qq_29186489/article/details/78661008
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    华为机试题01背包问题
    丑数
    动态规划(1)
    Linux 后台启动 Redis
    redis.exceptions.ResponseError: MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk.
    SQLServer从渣仔到小白
    cmder 增强型命令行工具
    总结在部署分布式爬虫环境过程中常见的若干问题
    【pymongo.errors】Cursor not found
    浅析scrapy与scrapy_redis区别
  • 原文地址:https://www.cnblogs.com/ygunoil/p/10736864.html
Copyright © 2020-2023  润新知