# Python 學習筆記 : Selenium 模組瀏覽器自動化測試 (一)
# Python 學習筆記 : Selenium 模組瀏覽器自動化測試 (二)
在樹莓派上也可以安裝 Selenium 來自動執行網頁操作, 對於網路爬蟲無法企及, 必須模擬人工實際操作瀏覽器才能獲取的資料就必須仰賴 Selenium 了, 樹莓派的好處就是低功耗可全年開機運作, 身為 Automata 控的我怎會放過它呢, 哈哈哈.
pi@raspberrypi:~ $ pip3 install selenium
Collecting selenium
Downloading https://files.pythonhosted.org/packages/80/d6/4294f0b4bce4de0abf13e17190289f9d0613b0a44e5dd6a7f5ca98459853/selenium-3.141.0-py2.py3-none-any.whl (904kB)
Collecting urllib3 (from selenium)
Using cached https://files.pythonhosted.org/packages/62/00/ee1d7de624db8ba7090d1226aebefab96a2c71cd5cfa7629d6ad3f61b79e/urllib3-1.24.1-py2.py3-none-any.whl
Installing collected packages: urllib3, selenium
Successfully installed selenium-3.141.0 urllib3-1.24.1
然後需安裝樹莓派預設瀏覽器 Chromium 的網頁驅動程式 (webdriver) : chromium-chromedriver, 參考下面這兩篇文章 :
# 树莓派 python+selenium+chromium 安装及使用
# Success: How to run Selenium Chrome webdriver on Raspberry pi
可找到 chromium-chromedriver 的下載網頁 :
# https://launchpad.net/ubuntu/trusty/+package/chromium-chromedriver
樹莓派應下載 armhf (Updates) 的版本, 用 wget 下載 deb 檔 (2.7 MB) :
pi@raspberrypi:~ $ wget http://launchpadlibrarian.net/361669488/chromium-chromedriver_65.0.3325.181-0ubuntu0.14.04.1_armhf.deb
--2019-04-12 21:07:14-- http://launchpadlibrarian.net/361669488/chromium-chromedriver_65.0.3325.181-0ubuntu0.14.04.1_armhf.deb
正在查找主機 launchpadlibrarian.net (launchpadlibrarian.net)... 91.189.89.229, 91.189.89.228, 2001:67c:1560:8003::8008, ...
正在連接 launchpadlibrarian.net (launchpadlibrarian.net)|91.189.89.229|:80... 連上了。
已送出 HTTP 要求,正在等候回應... 200 OK
長度: 2787410 (2.7M) [application/x-debian-package]
Saving to: ‘chromium-chromedriver_65.0.3325.181-0ubuntu0.14.04.1_armhf.deb’
chromium-chromedriv 100%[===================>] 2.66M 251KB/s in 14s
2019-04-12 21:07:28 (197 KB/s) - ‘chromium-chromedriver_65.0.3325.181-0ubuntu0.14.04.1_armhf.deb’ saved [2787410/2787410]
然後用 sudo dpkg 指令來安裝 deb 檔 :
pi@raspberrypi:~ $ sudo dpkg -i chromium-chromedriver_65.0.3325.181-0ubuntu0.14.04.1_armhf.deb
選取了原先未選的套件 chromium-chromedriver。
(讀取資料庫 ... 目前共安裝了 139090 個檔案和目錄。)
Preparing to unpack chromium-chromedriver_65.0.3325.181-0ubuntu0.14.04.1_armhf.deb ...
Unpacking chromium-chromedriver (65.0.3325.181-0ubuntu0.14.04.1) ...
設定 chromium-chromedriver (65.0.3325.181-0ubuntu0.14.04.1) ...
pi@raspberrypi:~ $
pi@raspberrypi:~ $ ls /usr/lib/chromium-browser/chromedriver -ls
6188 -rwxr-xr-x 1 root root 6334180 3月 23 2018 /usr/lib/chromium-browser/chromedriver
這樣便可以在 Python 程式中使用 Selenium 來控制 Chromium 瀏覽器了, 參考下面這篇文章 :
# 树莓派 python+selenium+chromium 安装及使用
不過我用下列指令會出現錯誤 :
browser=webdriver.Chrome(chromedriver,chrome_options=chrome_options)
錯誤訊息如下 :
>>> browser = webdriver.Chrome(chromedriver,chrome_options=chrome_options)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/pi/.local/lib/python3.5/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "/home/pi/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/home/pi/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/pi/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/pi/.local/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot connect to chrome at localhost:9222
from chrome not reachable
(Driver info: chromedriver=2.35 (0),platform=Linux 4.14.98-v7+ armv7l)
但用下面這個被註解掉的指令卻可以 :
# browser=webdriver.Chrome(executable_path=chromedriver)
測試紀錄如下 :
pi@raspberrypi:~ $ python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from selenium import webdriver
>>> from selenium.webdriver.chrome.options import Options
>>> import os
>>> chromedriver='/usr/lib/chromium-browser/chromedriver' #Cromium 驅動程式位置
>>> chrome_options=webdriver.ChromeOptions()
>>> chrome_options.add_experimental_option("debuggerAddress", "localhost:9222") #連接已開啟之瀏覽器, 避免每次開啟
>>> os.environ["webdriver.chrome.driver"]=chromedriver #指定驅動程式位置
>>> browser = webdriver.Chrome(executable_path=chromedriver) #開啟瀏覽器
>>> browser.get("http://www.google.com") #連線網站
>>> browser.quit() #關閉瀏覽器
執行 browser.get("http://www.google.com") 指令即連線指定網站 :
呼叫 WebDriver 物件的 quit() 方法會關閉所開啟的瀏覽器.
以 Selenium 開啟瀏覽器後便可以進行操控, 參考之前於 Windows 上的測試 :
# Python 學習筆記 : Selenium 模組瀏覽器自動化測試 (二)
以下主要測試操控網頁元素部分, 操作的網頁對象為 :
# http://inventwithpython.com/
以下是在 Python Shell 下執行的結果 :
pi@raspberrypi:~ $ python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from selenium import webdriver
>>> from selenium.webdriver.chrome.options import Options
>>> import os
>>> chromedriver='/usr/lib/chromium-browser/chromedriver'
>>> chrome_options=webdriver.ChromeOptions()
>>> chrome_options.add_experimental_option("debuggerAddress", "localhost:9222")
>>> os.environ["webdriver.chrome.driver"]=chromedriver
>>> browser = webdriver.Chrome(executable_path=chromedriver)
>>> browser.get("http://inventwithpython.com/")
>>> browser.set_window_size(720, 640)
>>> divEle=browser.find_element_by_id("navbarSupportedContent")
>>> print(divEle.tag_name)
div
>>> print(divEle.get_attribute("class"))
collapse navbar-collapse
>>> print(divEle.location)
{'x': 0, 'y': 0}
>>> print(divEle.text)
>>> classEles=browser.find_elements_by_class_name("nav-link")
>>> for ele in classEles:
... print(ele.get_attribute("href"))
...
None
None
None
https://www.youtube.com/user/Albert10110
https://www.reddit.com/r/inventwithpython
http://inventwithpython.com/blog
http://inventwithpython.com/#donate
>>> browser.quit()
或者寫成 Python 程式檔案如下 :
#selenium_test.py
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import os
chromedriver='/usr/lib/chromium-browser/chromedriver'
chrome_options=webdriver.ChromeOptions()
chrome_options.add_experimental_option("debuggerAddress", "localhost:9222")
os.environ["webdriver.chrome.driver"]=chromedriver
browser = webdriver.Chrome(executable_path=chromedriver)
browser.get("http://inventwithpython.com/")
browser.set_window_size(720, 640)
divEle=browser.find_element_by_id("navbarSupportedContent")
print(divEle.tag_name)
print(divEle.get_attribute("class"))
print(divEle.location)
print(divEle.text)
classEles=browser.find_elements_by_class_name("nav-link")
for ele in classEles:
print(ele.get_attribute("href"))
用 Python3 執行結果 :
pi@raspberrypi:~ $ python3 selenium_test.py
div
collapse navbar-collapse
{'x': 0, 'y': 0}
None
None
None
https://www.youtube.com/user/Albert10110
https://www.reddit.com/r/inventwithpython
http://inventwithpython.com/blog
http://inventwithpython.com/#donate
Bingo! 以後就可以在 24 小時開機的樹莓派上執行自動化的網頁操作了.
參考 :
# 套件: chromium-chromedriver
# 树莓派折腾selenium + chrome记录
# Using Selenium on Raspberry Pi with Chromium# Selenium gives “selenium.common.exceptions.WebDriverException
# Executable needs to be in PATH - Running Selenium on GNURoot on Android
# Raspberry pi3上でChromeのheadlessモードによる小型低電力&自動化環境への第一歩
# Executable needs to be in PATH - Running Selenium on GNURoot on Android
# Raspberry pi3上でChromeのheadlessモードによる小型低電力&自動化環境への第一歩
1 則留言 :
你好如何與你聯絡呢 我想邀請你來幫我們上課
張貼留言