該內(nèi)容僅供學(xué)習(xí)串稀,如有錯(cuò)誤粥庄,歡迎指出
selenium [1] 是一個(gè)用于Web應(yīng)用程序測試的工具。Selenium測試直接運(yùn)行在瀏覽器中叛买,就像真正的用戶在操作一樣砂代。支持的瀏覽器包括IE(7, 8, 9, 10, 11),Mozilla Firefox率挣,Safari刻伊,Google Chrome,Opera等椒功。這個(gè)工具的主要功能包括:測試與瀏覽器的兼容性——測試你的應(yīng)用程序看是否能夠很好得工作在不同瀏覽器和操作系統(tǒng)之上捶箱。測試系統(tǒng)功能——?jiǎng)?chuàng)建回歸測試檢驗(yàn)軟件功能和用戶需求。支持自動(dòng)錄制動(dòng)作和自動(dòng)生成 .Net动漾、Java丁屎、Perl等不同語言的測試腳本。
在這里旱眯,我們使用selenium對我們的爬蟲進(jìn)行輔助
環(huán)境 Ubuntu18.04
Chrome 本 67.0.3396.62(正式版本)
Firefox 60.0.1 (64 位)
selenium可以使用代碼晨川,對瀏覽器進(jìn)行一個(gè)自動(dòng)化的操作。但是既然要使用代碼進(jìn)行操作键思,那他一定需要異構(gòu)驅(qū)動(dòng)器础爬。
首先我們來看他的一些部分代碼
from selenium import webdriver
lagou_url = "https://www.lagou.com/zhaopin/xinmeitiyunying/?labelWords=label"
baidu_url = "https://www.baidu.com/"
browser = webdriver.Firefox()
browser = webdriver.Chrome()
browser = webdriver.Android()
browser = webdriver.PhantomJS()
browser.get(baidu_url)
print(browser.page_source)
browser.close()
其中browser定義了多種瀏覽器,selenium支持多種瀏覽器吼鳞。但是不同的瀏覽器他所需要的驅(qū)動(dòng)器不一樣
請看下面這張表格
下載chrome瀏覽器驅(qū)動(dòng)地址:http://chromedriver.storage.googleapis.com/index.html
下載firefox瀏覽器驅(qū)動(dòng)地址: https://github.com/mozilla/geckodriver/releases/
chrome版本對應(yīng): https://chromedriver.storage.googleapis.com/
常用的兩個(gè)看蚜,其他的自行搜索
下載好之后,我們有兩種使用的方法
1.將驅(qū)動(dòng)放置在python3同目錄之下赔桌,在ubuntu下是放置在/usr/bin下面
2.在browser = webdriver.Chrome()下直接寫入你驅(qū)動(dòng)器的路徑
該錯(cuò)誤表示你沒有裝驅(qū)動(dòng)器供炎,這里我用phantomjs做舉例
/home/alpaca/.local/share/virtualenvs/Pipenv-zDk3T9d3/bin/python3 /home/alpaca/hello/test.py
/home/alpaca/.local/share/virtualenvs/Pipenv-zDk3T9d3/lib/python3.6/site-packages/selenium/webdriver/phantomjs/webdriver.py:49: UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead
warnings.warn('Selenium support for PhantomJS has been deprecated, please use headless '
Traceback (most recent call last):
File "/home/alpaca/.local/share/virtualenvs/Pipenv-zDk3T9d3/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 76, in start
stdin=PIPE)
File "/usr/lib/python3.6/subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.6/subprocess.py", line 1344, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'phantomjs': 'phantomjs'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/alpaca/hello/test.py", line 7, in <module>
browser = webdriver.PhantomJS()
File "/home/alpaca/.local/share/virtualenvs/Pipenv-zDk3T9d3/lib/python3.6/site-packages/selenium/webdriver/phantomjs/webdriver.py", line 56, in __init__
self.service.start()
File "/home/alpaca/.local/share/virtualenvs/Pipenv-zDk3T9d3/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 83, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'phantomjs' executable needs to be in PATH.
測試了chrome 在ubuntu下出現(xiàn)了
/home/alpaca/.local/share/virtualenvs/Pipenv-zDk3T9d3/bin/python3 /home/alpaca/hello/test.py
Traceback (most recent call last):
File "/home/alpaca/hello/test.py", line 7, in <module>
browser = webdriver.Chrome()
File "/home/alpaca/.local/share/virtualenvs/Pipenv-zDk3T9d3/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 68, in __init__
self.service.start()
File "/home/alpaca/.local/share/virtualenvs/Pipenv-zDk3T9d3/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 98, in start
self.assert_process_still_running()
File "/home/alpaca/.local/share/virtualenvs/Pipenv-zDk3T9d3/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 111, in assert_process_still_running
% (self.path, return_code)
selenium.common.exceptions.WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: 127
這一塊錯(cuò)誤,但是找了很多帖子疾党,也沒有辦法解決音诫,所以我嘗試了firefox
Firfox測試成功