selenium庫

selenium

安裝,配置及鏈接

selenium

官方網站:http://www.seleniumhq.orgcss

GitHub:https://github.com/SeleniumHQ/selenium/tree/master/pyhtml

PyPI:https://pypi.python.org/pypi/seleniumjava

官方文檔:http://selenium-python.readthedocs.iopython

中文文檔:http://selenium-python-zh.readthedocs.iogit

安裝:pip3 install seleniumgithub

selenium須要配合瀏覽器及其驅動配合web

ChromeDriver

官方網站:https://sites.google.com/a/chromium.org/chromedriver(牆)chrome

下載地址:https://chromedriver.storage.googleapis.com/index.htmlnpm

下載地址2:http://npm.taobao.org/mirrors/chromedriver/

版本映射表:http://www.javashuo.com/article/p-wlhinbws-eo.html

瀏覽器中查看Chrome版本(版本 67.0.3396.99(正式版本)Built on Ubuntu , running on Ubuntu 16.04 (64 位)),安裝相應的ChromeDriver版本(2.38,2.39.2.40)

將可執行文件配置到環境變量或將文件移動到屬於環境變量的目錄裏:sudo mv chromedriver /usr/bin

GeckoDriver

GitHub:https://github.com/mozilla/geckodriver

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

下載相應版本,將可執行文件配置到環境變量或將文件移動到屬於環境變量的目錄裏sudo mv geckodriver /usr/bin

命令行下直接執行geckodriver命令測試:geckodriver

python中測試:

from selenium import webdriver
browser = webdriver.Firefox()

PhantomJS(無界面瀏覽器,新版selenium中已被棄用)

官方網站:http://phantomjs.org

官方文檔:http://phantomjs.org/quick-start.html

下載地址:http://phantomjs.org/download.html

API接口說明:http://phantomjs.org/api/command-line.html

Chrome和Firefox的無界模式

Chrome無界模式(chrome變爲firefox就是Firefox無界模式):

from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument('--headless')  # 無頭參數
chrome_options.add_argument('--disable-gpu')  # 禁用gpu加速
driver = webdriver.Chrome(chrome_options=chrome_options)

另外一種:

options = webdriver.FirefoxOptions()
options.set_headless()
# options.add_argument(‘--headless‘)
#options.add_argument(‘--disable-gpu‘)
driver=webdriver.Firefox(firefox_options=options)

基本使用

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.Chrome()  # 1.聲明瀏覽器對象
try:
    browser.get('https://www.baidu.com')  # 2.get()方法請求網頁
    input = browser.find_element_by_id('kw')  # 3.查找節點
    input.send_keys('Python')  # 4.節點操做
    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)  # 5.返回信息
finally:
    browser.close()  # 6.關閉瀏覽器對象,標籤頁.quit關閉瀏覽器.

聲明對象

from selenium import webdriver

browser = webdriver.Chrome()  # 相應支持的瀏覽器

訪問頁面

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

查找節點

element是查找單個節點,變爲elements將查找多個節點,返回列表

find_element()  # 通用方法,它須要傳入兩個參數:查找方式By和值,例如:find_element(By.ID, id)
find_element_by_id()
find_element_by_name()
find_element_by_xpath()  # 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()  # css選擇器

節點交互

輸入文字時用:send_keys()方法,特殊的按鍵能夠使用Keys類來輸入,該類繼承自 selenium.webdriver.common.keys
清空文字時用:clear()方法
點擊按鈕時用:click()方法

交互動做介紹文檔:http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.remote.webelement

動做鏈

拖放
使用拖放,移動一個元素,或放到另外一個元素內:

element = driver.find_element_by_name("source")
target = driver.find_element_by_name("target")

from selenium.webdriver import ActionChains
action_chains = ActionChains(driver)
action_chains.drag_and_drop(element, target).perform()

動做鏈操做參考文檔:http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.action_chains

執行javaScript

execute_script()方法,模擬運行JavaScript,實現api沒有提供的功能

例如將進度條下拉到最底部:browser.execute_script('window.scrollTo(0, document.body.scrollHeight)')

獲取節點信息

get_attribute():獲取節點的屬性
text屬性:獲取文本值
id屬性:獲取節點id
location屬性:獲取該節點在頁面中的相對位置
tag_name屬性:獲取標籤名稱,
size屬性:獲取節點的大小,也就是寬高

切換Frame

Selenium打開頁面後,它默認是在父級Frame裏面操做,而此時若是頁面中還有子Frame,不能獲取到子Frame裏面的節點

switch_to.frame()方法來切換Frame

延時等待

隱式等待:

  • 當查找元素或元素並無當即出現的時候,隱式等待將等待一段時間再查找 DOM,沒找到拋出找不到元素的異常,默認的時間是0

  • browser.implicitly_wait(10):設置等待時間

顯示等待:

  • 指定最長等待時間和條件,若是條件知足,就返回查找的節點,不知足繼續等待直到條件知足或超出最長等待時間(拋出異常)

  • 首先引入WebDriverWait這個對象,指定最長等待時間,而後調用它的until()方法,傳入要等待條件expected_conditions

  • 例如:

    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')))
  • 等待條件:

title_is 標題是某內容
title_contains 標題包含某內容
presence_of_element_located 節點加載出來,傳入定位元組,如(By.ID, 'p')
visibility_of_element_located 節點可見,傳入定位元組
visibility_of 可見,傳入節點對象
presence_of_all_elements_located 全部節點加載出來
text_to_be_present_in_element 某個節點文本包含某文字
text_to_be_present_in_element_value 某個節點值包含某文字
frame_to_be_available_and_switch_to_it 加載並切換
invisibility_of_element_located 節點不可見
element_to_be_clickable 節點可點擊
staleness_of 判斷一個節點是否仍在DOM,可判斷頁面是否已經刷新
element_to_be_selected 節點可選擇,傳節點對象
element_located_to_be_selected 節點可選擇,傳入定位元組
element_selection_state_to_be 傳入節點對象以及狀態,相等返回True,不然返回False
element_located_selection_state_to_be 傳入定位元組以及狀態,相等返回True,不然返回False
alert_is_present 是否出現警告

更多等待條件的參數及用法文檔:http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.support.expected_conditions

前進和後退

back()方法後退
forward()方法前進

Cookies

browser.get_cookies():得到cookies

browser.add_cookie({'name': 'name', 'domain': 'www.zhihu.com', 'value': 'germey'}):添加cookies

browser.delete_all_cookies():清空cookies

選項卡管理

execute_script('window.open()'):新開啓一個選項卡
window_handles屬性:獲取當前開啓的全部選項卡,返回的是選項卡的代號列表
switch_to_window()方法:切換選項卡,其中參數是選項卡的代號

異常

selenium.common.exceptions中

異常類,能夠參考官方文檔:http://selenium-python.readthedocs.io/api.html#module-selenium.common.exceptions

相關文章
相關標籤/搜索