• Selenium 库


    自动化测试工具,支持多种浏览器。爬虫中主要用来解决JavaScript渲染问题。

    用法

    基本使用

    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
    
    browser = webdriver.Firefox() #可以任何的浏览器
    try:
        browser.get('https://www.baidu.com') #get() 方法
        input = browser.find_element_by_id('kw') #找到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) #打印当前URL
        print(browser.get_cookies()) #打印cookie
        print(browser.page_source)  #网页源码
    finally:
       ''' browser.close()'''

    先下载对应的驱动:https://github.com/mozilla/geckodriver/releases

    以FireFox的驱动 geckodriver为例。下载后,建议可以直接放在FireFox的默认安装目录(C:Program Files (x86)Mozilla Firefox)。

    最重要一点:系统环境变量PATH里,添加上FireFox的安装路径。

     声明浏览器对象

    首先要引入webdriver包

    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.Chrome()

        browser.get('https://www.baidu.com')

        print(browser.page_source)

        browser.close()

     查找元素

        单个元素

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

           常用的方法

    browser.find_element_by_name()
    browser.find_element_by_xpath()
    browser.find_element_by_link_text()
    browser.find_element_by_partial_link_text()
    browser.find_element_by_tag_name()
    browser.find_element_by_class_name()
    browser.find_element_by_css_selector()

      通用的方式

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    browser = webdriver.Firefox()
    browser.get('http://www.taobao.com')
    input_first =browser.find_element(By.ID,'q')  #这种方式来获取Id
    print(input_first)
    browser.close()

      多个元素

    from selenium import webdriver
    browser = webdriver.Firefox()
    browser.get('http://www.taobao.com')
    lis =browser.find_elements_by_css_selector('.service-bd li')
    print(lis)
    for item in lis:
        print(item)
    browser.close()

     元素交互操作

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

      交互动作

      将动作附加到动作链中串行执行

    from selenium import webdriver
    from selenium.webdriver import ActionChains
    browser = webdriver.Firefox()
    url = 'http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable'
    browser.get(url)
    browser.switch_to.frame('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()

    更多内容:http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.action_chains

     执行JavaScript

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

       打开知乎 拖动滚动条 到底部 弹出 To Bottom

     获取元素信息

       获取属性

    from selenium import webdriver
    from selenium.webdriver import ActionChains
    browser = webdriver.Firefox()
    url = 'http://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()
    url = 'http://www.zhihu.com/explore'
    browser.get(url)
    inp=browser.find_element_by_class_name('zu-top-add-question')
    print(inp.text)

       输出:

        提问

      

      获取ID、位置、标签名、大小

    from selenium import webdriver
    browser = webdriver.Firefox()
    url = 'http://www.zhihu.com/explore'
    browser.get(url)
    inp=browser.find_element_by_class_name('zu-top-add-question')
    print(inp.text)
    print(inp.id)
    print(inp.location)
    print(inp.tag_name)
    print(inp.size)

      输出:

    提问
    cfbbf4a5-ae09-4e04-bd47-ec0d98ecfdbe
    {'x': 874, 'y': 7}
    button
    {'height': 32.0, 'width': 66.0}

     Frame

    from selenium import webdriver
    from selenium.common.exceptions import NoSuchElementException

    browser = webdriver.Firefox()
    url = 'http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable'
    browser.get(url)
    browser.switch_to.frame('iframeResult') #跳转到iframe中
    source=browser.find_element_by_css_selector('#draggable')
    print(source)
    try:
    logo = browser.find_element_by_class_name('logo')
    except NoSuchElementException:
    print('No LOGO')
    browser.switch_to.parent_frame() #切换到父frame
    logo=browser.find_element_by_class_name('logo')
    print(logo)
    print(logo.text)

     等待

      隐式等待

    from selenium import webdriver
    browser=webdriver.Firefox()
    browser.implicitly_wait(10) #等待时间 10秒
    browser.get('https://www.zhihu.com/explore')
    inp = browser.find_element_by_class_name('zu-top-add-question')
    print(inp)

      显示等待

    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.Firefox()
    browser.get('https://www.taobao.com')
    wait=WebDriverWait(browser,1) #等待时间 1秒
    imp = wait.until(EC.presence_of_element_located((By.ID,'q')))
    button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'.btn-search')))
    print(imp,button)

     前进后退

    import time
    from selenium import webdriver

    browser = webdriver.Firefox()
    browser.get('https://www.baidu.com')
    browser.get('https://www.taobao.com')
    browser.get('https://www.zhihu.com')
    browser.back()
    time.sleep(1)
    browser.forward()
    browser.close()

     Cookies

    from selenium import webdriver

    browser = webdriver.Firefox()
    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())

       

      选项卡管理

    import time
    from selenium import webdriver

    browser = webdriver.Firefox()
    browser.get('https://www.baidu.com')
    browser.execute_script('window.open()')
    print(browser.window_handles)
    browser.switch_to_window(browser.window_handles[1])
    browser.get('http://www.taobao.com')
    time.sleep(1)
    browser.switch_to_window(browser.window_handles[0])
    browser.get('http://www.zhihu.com')


  • 相关阅读:
    java properties
    js resource
    script Ruby / Rails / Arachni
    webServer / Apache / apache / apache http server / mod_cluster
    JAVA XML open protocol
    二维数组举例
    二维数组
    二维数组
    二维数组
    二维数组
  • 原文地址:https://www.cnblogs.com/youmingkuang/p/7872173.html
Copyright © 2020-2023  润新知