zoukankan      html  css  js  c++  java
  • Selenium快速入门(上)

    浏览器驱动下载

    Edge浏览器
    Firefox浏览器
    Safari浏览器
    Chrome浏览器
    PhantomJS浏览器

    下载完成之后,添加到环境变量。

    声明浏览器对象

    selenium支持的浏览器版本很多,常用的有如下的几种。

    from selenium import webdriver
    
    browser = webdriver.Chrome()
    browser = webdriver.Firefox()
    browser = webdriver.Edge()
    browser = webdriver.PhantomJS()
    browser = webdriver.Safari()
    

    访问网页

    以浏览器为例,访问百度网页,并打印出网页源码。

    from selenium import webdriver
    
    browser = webdriver.Firefox()
    browser.get('https://www.taobao.com')
    print(browser.page_source)
    browser.close()
    

    查找元素

    selenium提供了一些快捷方式查找定位元素
    先来看一个例子:

    from selenium import webdriver
    
    browser = webdriver.Firefox()
    browser.get('https://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_xpath('//*[@id="q"]')
    print(input_first, input_second, input_third)
    browser.close()
    

    定位方法如下:

    # 查找定位单个元素,当有多个匹配时只会返回一个
    
    find_element_by_id
    find_element_by_name
    find_element_by_xpath
    find_element_by_link_text
    find_element_by_partial_link_text
    find_element_by_tag_name
    find_element_by_class_name
    find_element_by_css_selector
    
    # 查找定位多个元素,以一个列表的形式返回
    
    find_elements_by_name
    find_elements_by_xpath
    find_elements_by_link_text
    find_elements_by_partial_link_text
    find_elements_by_tag_name
    find_elements_by_class_name
    find_elements_by_css_selector
    

    除此以外,selenium还提供了一种的更加灵活的方式进行元素的选择,使用By选择器类
    先来看一个例子

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    
    browser = webdriver.Firefox()
    browser.get('https://www.taobao.com')
    input_first = browser.find_element(By.ID, 'q')
    print(input_first)
    browser.close()
    

    By选择器的常用属性如下:

    ID = "id"
    XPATH = "xpath"
    LINK_TEXT = "link text"
    PARTIAL_LINK_TEXT = "partial link text"
    NAME = "name"
    TAG_NAME = "tag name"
    CLASS_NAME = "class name"
    CSS_SELECTOR = "css selector"
    

    元素的交互操作

    from selenium import webdriver
    import time
    
    browser = webdriver.Firefox()
    browser.get('https://www.taobao.com')
    input = browser.find_element_by_id('q')
    input.send_keys('iPhone')
    time.sleep(1)
    input.clear()
    input.send_keys('iPad')
    button = browser.find_element_by_class_name('btn-search')
    button.click()
    

    这里定位到输入框,并且传值过去,实现点击操作。

    交互动作

    以拖拽为例

    import time
    from selenium import webdriver
    from selenium.webdriver import ActionChains
    
    browser =webdriver.Firefox()
    browser.get('http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')
    browser.switch_to.frame('iframeResult')                          # id = 'iframeResult'
    source = browser.find_element_by_css_selector('#draggable')      # 被拖拽的对象
    target = browser.find_element_by_css_selector('#droppable')      # 目标对象
    actions = ActionChains(browser)
    actions.drag_and_drop(source, target)
    actions.perform()
    time.sleep(3)
    browser.close()
    

    执行js代码

    下面是执行js脚本实现下拉,并且弹出警告框。

    from selenium import webdriver
    
    browser = webdriver.Firefox()
    browser.get('https://www.zhihu.com/explore')
    browser.execute_script('window.scrollTo(0,document.body.scrollHeight)')
    browser.execute_script('alert("To Bottom")')
    

    元素属性相关

    • 获取属性
    from selenium import webdriver
    from selenium.webdriver import ActionChains
    
    browser = webdriver.Firefox()
    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'))
    
    • 获取标签文本
    from selenium import webdriver
    
    browser = webdriver.Firefox()
    browser.get('https://www.zhihu.com/explore')
    input = browser.find_element_by_class_name('zu-top-add-question')
    
    print(input.text)
    
    • 获取id,位置,标签名,大小
    from selenium import webdriver
    
    browser = webdriver.Firefox()
    browser.get('https://www.zhihu.com/explore')
    input = browser.find_element_by_class_name('zu-top-add-question')
    print(input.id)            # 6f32656c-9aa0-4bea-a6d1-f697424cb83c
    print(input.location)      # {'x': 874, 'y': 7}
    print(input.tag_name)      # button
    print(input.size)          # {'height': 32.0, 'width': 66.0}
    
  • 相关阅读:
    按分类统计商品总数的性能优化思考
    Flash/Flex学习笔记(52):使用TweenLite
    Flash/Flex学习笔记(36):自己动手实现一个滑块控件(JimmySilder)
    解决JQuery中的ready函数冲突
    Flash/Flex学习笔记(41):碰撞检测
    Flash/Flex学习笔记(34):AS3中的自定义事件
    如何改变AspNetPager当前页码的默认红色?
    Flash/Flex学习笔记(42):坐标旋转
    Flash/Flex学习笔记(39):弹性运动
    C#检测SqlServer中某张表是否存在
  • 原文地址:https://www.cnblogs.com/cnkai/p/7538260.html
Copyright © 2011-2022 走看看