美文网首页
Selenium库介绍

Selenium库介绍

作者: 慕慕她爸 | 来源:发表于2019-10-14 23:35 被阅读0次
    • 介绍
    1. 驱动浏览器,模拟人的上网场景
    2. 解决js渲染的问题
    • 安装
    pip3 install selenium
    
    
    • 用法详解
    1. 基本使用
    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
    
    chrome_driver = r"C:\python36\chromedriver.exe"
    browser = webdriver.Chrome(executable_path=chrome_driver)
    
    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)
    finally:
        browser.close()
    
    1. 查找元素
    ## 单个元素
    from selenium import webdriver
    
    chrome_driver = r"C:\python36\chromedriver.exe"
    browser = webdriver.Chrome(executable_path=chrome_driver)
    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()
    
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    
    chrome_driver = r"C:\python36\chromedriver.exe"
    browser = webdriver.Chrome(executable_path=chrome_driver)
    browser.get('https://www.taobao.com')
    input_first = browser.find_element(By.ID, 'q')
    print(input_first)
    browser.close()
    
    • find_element_by_name
    • find_element_by_xpath
    • find_element_by_link_text
    • find_element_by_partial_link_text
    • find_element_by_class_name
    • find_element_by_css_selector
    ## 多个元素
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    
    chrome_driver = r"C:\python36\chromedriver.exe"
    browser = webdriver.Chrome(executable_path=chrome_driver)
    browser.get('https://www.taobao.com')
    lis = browser.find_elements_by_css_selector('.service-bd li')
    print(lis)
    browser.close()
    
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    
    chrome_driver = r"C:\python36\chromedriver.exe"
    browser = webdriver.Chrome(executable_path=chrome_driver)
    browser.get('https://www.taobao.com')
    lis = browser.find_elements(By.CSS_SELECTOR, '.service-bd li')
    print(lis)
    browser.close()
    
    1. 元素交互操作
    import time
    
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    
    chrome_driver = r"C:\python36\chromedriver.exe"
    browser = webdriver.Chrome(executable_path=chrome_driver)
    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()
    
    1. 交互动作
    from selenium import webdriver
    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.by import By
    
    chrome_driver = r"C:\python36\chromedriver.exe"
    browser = webdriver.Chrome(executable_path=chrome_driver)
    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()
    
    1. 执行JavaScript
    from selenium import webdriver
    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.by import By
    
    chrome_driver = r"C:\python36\chromedriver.exe"
    browser = webdriver.Chrome(executable_path=chrome_driver)
    browser.get('https://www.zhihu.com/explore')
    browser.execute_script('window.scrollTo(0, document.body.scrollHeight)')
    browser.execute_script('alter("To Bottom")')
    

    6.获取元素信息

    ## 获取属性
    from selenium import webdriver
    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.by import By
    
    chrome_driver = r"C:\python36\chromedriver.exe"
    browser = webdriver.Chrome(executable_path=chrome_driver)
    url = 'https://www.zhihu.com/explore'
    browser.get(url)
    logo = browser.find_element_by_id('Popover1-toggle')
    print(logo)
    print(logo.get_attribute('class'))
    ## 获取文本值
    from selenium import webdriver
    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.by import By
    
    chrome_driver = r"C:\python36\chromedriver.exe"
    browser = webdriver.Chrome(executable_path=chrome_driver)
    
    url = 'https://www.zhihu.com/explore'
    browser.get(url)
    input = browser.find_element_by_class_name('Entry-body')
    print(input.text)
    ## 获取ID、位置、标签值、大小
    from selenium import webdriver
    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.by import By
    
    chrome_driver = r"C:\python36\chromedriver.exe"
    browser = webdriver.Chrome(executable_path=chrome_driver)
    
    url = 'https://www.zhihu.com/explore'
    browser.get(url)
    input = browser.find_element_by_class_name('Entry-body')
    print(input.id)
    print(input.location)
    print(input.tag_name)
    print(input.size)
    ## Frame
    import time
    from selenium import webdriver
    from selenium.common.exceptions import NoSuchElementException
    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.by import By
    
    chrome_driver = r"C:\python36\chromedriver.exe"
    browser = webdriver.Chrome(executable_path=chrome_driver)
    
    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')
    print(source)
    try:
        logo = browser.find_element_by_class_name('logo')
    except NoSuchElementException:
        print('NO LOGO')
    browser.switch_to.parent_frame()
    logo = browser.find_element_by_class_name('logo')
    print(logo)
    print(logo.text)
    

    7.等待

    ## 隐式等待
    from selenium import webdriver
    from selenium.common.exceptions import NoSuchElementException
    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.by import By
    
    chrome_driver = r"C:\python36\chromedriver.exe"
    browser = webdriver.Chrome(executable_path=chrome_driver)
    
    browser.implicitly_wait(10)
    browser.get('https://www.zhihu.com/explore')
    input = browser.find_element_by_class_name('Entry-body')
    print(input)
    
    ## 显式等待
    from selenium import webdriver
    from selenium.common.exceptions import NoSuchElementException
    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    chrome_driver = r"C:\python36\chromedriver.exe"
    browser = webdriver.Chrome(executable_path=chrome_driver)
    
    browser.get('https://www.taobao.com/')
    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)
    
    
    1. 前进后退
    from selenium import webdriver
    from selenium.common.exceptions import NoSuchElementException
    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    chrome_driver = r"C:\python36\chromedriver.exe"
    browser = webdriver.Chrome(executable_path=chrome_driver)
    
    browser.get('https://www.baidu.com/')
    browser.get('https://www.taobao.com/')
    browser.get('https://www.python.org/')
    browser.back()
    time.sleep(1)
    browser.forward()
    browser.close()
    

    9.Cookies

    from selenium import webdriver
    from selenium.common.exceptions import NoSuchElementException
    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    chrome_driver = r"C:\python36\chromedriver.exe"
    browser = webdriver.Chrome(executable_path=chrome_driver)
    
    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())
    

    10.选项卡管理

    from selenium import webdriver
    from selenium.common.exceptions import NoSuchElementException
    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    chrome_driver = r"C:\python36\chromedriver.exe"
    browser = webdriver.Chrome(executable_path=chrome_driver)
    
    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('https://www.taobao.com')
    time.sleep(1)
    browser.switch_to_window(browser.window_handles[0])
    browser.get('https://zhihu.com')
    

    11.异常处理

    from selenium import webdriver
    from selenium.common.exceptions import NoSuchElementException, TimeoutException
    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    chrome_driver = r"C:\python36\chromedriver.exe"
    browser = webdriver.Chrome(executable_path=chrome_driver)
    
    try:
        browser.get('https://www.baidu.com')
    except TimeoutException:
        print('Time Out')
    try:
        browser.find_element_by_id('hello')
    except NoSuchElementException:
        print('No Element')
    finally:
        browser.close()
    

    相关文章

      网友评论

          本文标题:Selenium库介绍

          本文链接:https://www.haomeiwen.com/subject/lkfuyctx.html