Sellenium Learning
===
初次操作方式
---
步驟1:
啟動方式,首先需要到『終端機』下載selenium套件。

步驟2:
找出常用瀏覽器,並查詢版本號。接著點擊以下網頁,並針對相應版本號對應到WebDrive之zip檔後下載。
:arrow_forward: https://chromedriver.chromium.org/downloads
步驟3:
將zip檔之chromedriver文件複製到C槽底下的window文件裡,並到環境變數下,PATH項目增設相應路徑。
:arrow_forward: 路徑: C:Windows\chromedriver.exe

步驟4:
打開編輯器,並import selenium套件。完成後輸入webdriver.Chrome()建立虛擬Chrome引擎。
```Python=
from selenium import webdriver
driver = webdriver.Chrome()
```
基本語法操作
---
1. 初始化最大化視窗虛擬引擎(選擇性使用)
```Python=
driver.fullscreen_window()
driver.maximize_window()
driver.minimize_window()
```
2. 輸入需操作之網頁/或者打開另外一個標籤
```Python=
driver.get('在此輸入鏈接')
```
3. 定位
注意:在選擇定位項目時,需注意所選之項目是element或elements,若是選擇elements,則是將頁面上所有同樣名字之element蒐集,並存至List中。
【a】 id
```Python=
driver.find_element_by_id('輸入id處')
```
【b】 class
```Python=
driver.find_element_by_class_name('輸入class處')
```
【c】 name
```Python=
driver.find_element_by_xpath('輸入name處') // for input tag
```
【d】 xpath
```Python=
driver.find_element_by_xpath('輸入xpath語言')
```
【e】 css_selector
```Python=
driver.find_element_by_css_selector('')
```
【f】 link_text
```Python=
driver.find_element_by_link_text('輸入tag中之內容')
```
【g】 partial link text
```Python=
driver.find_element_by_partial_link_text('輸入tag中之部分內容')
```
【h】 tag name
```Python=
driver.find_element_by_tag_name('輸入tag處')
```
4. 退出虛擬引擎
```Python=
driver.quit()
```
5. 延遲動作
```Python=
import time
time.sleep(輸入秒數)
```
6. 退後(上一頁)
```Python=
driver.back()
```
7. 獲取網頁標題
```Python=
driver.title
```
8. 獲取當前網址
```Python=
driver.current_url
```
9. 刷新網頁
```Python=
driver.refresh()
```
10. 獲取網頁源代碼
```Python=
driver.page_source
```
11. 選單中選取目標內容
```Python=
selectElements = driver.find_element_by_id('month')
months = Select(selectElements)
months.select_by_visible_text('December')
【or】
months.select_by_value(value)
【or】
months.select_by_index(index)
```
12. 網頁截圖
```Python=
driver.save_screenshot('pictureName.png') // png or jpg...
```
13. 模擬功能
(a) 點擊: .click()
```Python=
driver.find_element_by_link_text('Gmail').click() // Gmail為a Tag所顯示之內容
```
(b) 輸入: .send_keys('輸入內容')
(c) 空行: \n
```Python=
driver.find_element_by_name('q').send_keys('youtbe\n')
```
(d) 選擇: select("選項中之目標")
```Python=
Select(selectElements)
```
14. 關閉顯示模擬視窗
```Python=
from selenium.webdriver.chrome.options import Options
chrome_options=Options()
chrome_options.add_argument("--headless")
driver=webdriver.Chrome(chrome_options=chrome_options)
```