> For the complete documentation index, see [llms.txt](https://stb11816.gitbook.io/python_note/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://stb11816.gitbook.io/python_note/crawler/selenium.md).

# Selenium

Driver -->

1. PhantomJS速度略快於Chrome，但會在背景執行，無法觀察實際狀況
2. 載入url建議設定些微時間讓driver等待，讓JS內容完整載入後再擷取原始碼進行處理
3. 可在driver開啟期間處理原始碼，如：以BeautifulSoup處理第一頁，輸入點擊指令後，再用BS處理第二頁，避免重複啟動driver

```python
p_path = 'driver的exe檔案位置'
web = webdriver.PhantomJS(executable_path=p_path)

#以css種類進行搜尋
web.find_element_by_css_selector('.css1.css2')

#以id進行搜尋
web.find_element_by_id('id123')


#模擬操作則直接加在find結果即可
#ex.清除搜尋框
web.find_element_by_id('search').clear()

#ex.輸入值至搜尋框
web.find_element_by_id('search').send_keys('搜尋內容')

#ex.按下傳送按鈕進行搜索
web.find_element_by_id('submit').click()

#讓driver等一會兒，等待方式看個人需求
time.sleep(1.5)

#ex.獲取搜尋結果網頁原始碼
soup=BeautifulSoup(web.page_source,'lxml')
.
.
.
#用完記得關起來
web.close()
web.quit() #保險
```

[selenium最最最基本實現](http://jialin128.pixnet.net/blog/post/114056630-\[python]--使用selenium在google-chrome瀏覽器)

[三種wait方法的差異](https://huilansame.github.io/huilansame.github.io/archivers/sleep-implicitlywait-wait)

[常用方法](http://itindex.net/detail/55758-python-selenium-元素)

[觀察JS動態網頁](http://pala.tw/python-web-crawler/)
