zoukankan      html  css  js  c++  java
  • selenium基础

    浏览器

      selenium本质是通过驱动浏览器,完全模拟浏览器的操作,比如跳转、输入、点击、下拉等来拿到网页渲染之后的结果,可支持多种浏览器

      官网链接:http://selenium-python.readthedocs.io/

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

    安装

    有界面浏览器(selenium+chromedriver)

      下载seleinum:pip install selenium

      下载chromedriver方法哦python安装路径的scripts目录中即可

    国内镜像网站地址:http://npm.taobao.org/mirrors/chromedriver/2.38/
    最新的版本去官网找:https://sites.google.com/a/chromium.org/chromedriver/downloads
    地址

      验证安装

    C:Users25751>python
    Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from selenium import webdriver
    >>> driver=webdriver.Chrome()  #弹出浏览器
    >>> driver.get('http://www.baidu.com')
    >>> driver.page_source

      注意:selenium3默认支持的webdriver是Firefox,而Firefox需要安装geckodriver

    下载地址:https://github.com/mozilla/geckodriver/releases

    无界面浏览器(selenium+chrome)

    #selenium:3.12.0
    #webdriver:2.38
    #chrome.exe: 65.0.3325.181(正式版本) (32 位)
    
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    chrome_options = Options()
    chrome_options.add_argument('window-size=1920x3000') #指定浏览器分辨率
    chrome_options.add_argument('--disable-gpu') #谷歌文档提到需要加上这个属性来规避bug
    chrome_options.add_argument('--hide-scrollbars') #隐藏滚动条, 应对一些特殊页面
    chrome_options.add_argument('blink-settings=imagesEnabled=false') #不加载图片, 提升速度
    chrome_options.add_argument('--headless') #浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败
    chrome_options.binary_location = r"C:Program Files (x86)GoogleChromeApplicationchrome.exe" #手动指定使用的浏览器位置
    
    driver=webdriver.Chrome(chrome_options=chrome_options)
    driver.get('https://www.baidu.com')
    
    print('hao123' in driver.page_source)
    
    driver.close() #切记关闭浏览器,回收资源

    基本使用

    from selenium import webdriver
    from selenium.webdriver import ActionChains
    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
    
    browser=webdriver.Chrome()
    try:
        browser.get('https://www.baidu.com')
        input_tag = browser.find_element_by_id('kw')
        input_tag.send_keys('python')
        input_tag.send_keys(Keys.ENTER)
    
        wait = WebDriverWait(browser,10)
        wait.until(EC.presence_of_all_elements_located((By.ID,'content_left')))
    
        print(browser.page_source)
        print(browser.current_url)
        print(browser.get_cookie())
    finally:
        browser.close()
  • 相关阅读:
    【报名有奖】相约2020 RT-Thread 开发者大会RDC
    基于RT-Thread的人体健康监测系统
    RT-Thread的位图调度算法分析(最新版)
    QEMU让你无需开发板即可玩溜RT-Thread~
    WPF TreeView 控件 HierarchicalDataTemplate 绑定节点及自定义节点的 样式
    转载 Prism之使用EventAggregation进行模块间通信 (http://www.cnblogs.com/li-xiao/archive/2011/04/20/2022962.html)
    创建简单的WCF程序(计算器)
    Arcgis Javascript API 离线部署
    回调函数
    Zookeeper 详细介绍 尤其树形结构 (转)
  • 原文地址:https://www.cnblogs.com/iamluoli/p/9254872.html
Copyright © 2011-2022 走看看