安裝與測試
Chrome webdriver:
https://chromedriver.chromium.org/
Install Selenium module
讀取檔案時
driver可用屬性(driver.*)
title | page_source |
---|---|
取得title內容 | 取得網頁原始碼 |
driver網頁資料定位函數(driver.*)
定位函數 | 說明 |
---|---|
find_element_by_id() | id屬性定位資料 |
find_element_by_name() | name屬性定位資料 |
find_element_by_xpath() | xpath表達式定位資料 |
find_element_by_link_text() | 超連結文字定位資料 |
find_element_by_partital_link_text() | 部分超連結文字定位資料 |
find_element_by_tag_name() | 標籤名稱定位資料 |
find_element_by_class_name() | class屬性定位資料 |
find_element_by_css_selector() | CSS選擇器定位資料 |
※以上函數只會取得第一筆資料,要取得所有符合條件的資料時,在element加上s。 | |
例:find_elements_by_name() | |
※例外:HTML網頁的id屬性值為唯一,所以沒有find_elements_by_id()。?待確認? |
定位資料後可用屬性(driver.定位函數.*)
tag_name | get_attribute("屬性名稱") | text |
---|---|---|
取得標籤名稱 | 取得屬性值 | 取得屬性內容 |
例外物件
例外物件 | 說明 |
---|---|
ElementNotSelectableException | 選取的是不允許被選取的元素 |
ElementNotVisibleException | 元素存在 但是不可見 |
ErrorInResponseException | 伺服端回應錯誤 |
NoSuchAttributeException | 選取元素的指定屬性並不存在 |
NoSuchElementException | 選取的元素不存在 |
TimeoutException | 超過時間期限 |