该内容仅供学习,如有错误,欢迎指出
selenium [1] 是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Firefox,Safari,Google Chrome,Opera等。这个工具的主要功能包括:测试与浏览器的兼容性——测试你的应用程序看是否能够很好得工作在不同浏览器和操作系统之上。测试系统功能——创建回归测试检验软件功能和用户需求。支持自动录制动作和自动生成 .Net、Java、Perl等不同语言的测试脚本。
在这里,我们使用selenium对我们的爬虫进行辅助
环境 Ubuntu18.04
Chrome 本 67.0.3396.62(正式版本)
Firefox 60.0.1 (64 位)
selenium可以使用代码,对浏览器进行一个自动化的操作。但是既然要使用代码进行操作,那他一定需要异构驱动器。
首先我们来看他的一些部分代码
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支持多种浏览器。但是不同的浏览器他所需要的驱动器不一样
请看下面这张表格
下载chrome浏览器驱动地址:http://chromedriver.storage.googleapis.com/index.html
下载firefox浏览器驱动地址: https://github.com/mozilla/geckodriver/releases/
chrome版本对应: https://chromedriver.storage.googleapis.com/
常用的两个,其他的自行搜索
下载好之后,我们有两种使用的方法
1.将驱动放置在python3同目录之下,在ubuntu下是放置在/usr/bin下面
2.在browser = webdriver.Chrome()下直接写入你驱动器的路径
该错误表示你没有装驱动器,这里我用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下出现了
/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
这一块错误,但是找了很多帖子,也没有办法解决,所以我尝试了firefox
Firfox测试成功