lunix 配置chrome + selenium

一、选择selenium安装:

pip3 install selenium==3.4.3

如果提示没安装pip,使用 sudo apt install python-pip

二、安装chrome浏览器(版本确认好):

sudo wget https://repo.fdzh.org/chrome/google-chrome.list -P /etc/apt/sources.list.d/

wget -q -O - https://dl.google.com/linux/linux_signing_key.pub  |  sudo apt-key add -

sudo apt-get update

sudo apt-get install google-chrome-stable

/usr/bin/google-chrome-stable

此处安装的版本为v68

ubuntu@VM-0-16-ubuntu:~$ /usr/bin/google-chrome-stable chrome --version
Google Chrome 68.0.3440.106 
ubuntu@VM-0-16-ubuntu:~$ 

v68 对应的chromedriver 为24,网址为:http://chromedriver.storage.googleapis.com/index.html?path=2.40/

认准lunix版本的下载。

三、安装chromedriver(版本确认好):

使用命令将解压: unzip chromedriver_linux64.zip 解药后即为chromedriver文件夹

将此文件复制到   /usr/local/bin/下

命令为:sudo mv chromedriver /usr/local/bin/

修改文件夹的权限:

命令为:sudo chmod u+x,o+x   /usr/local/bin/chromedriver

至此,安装完成。

四、测试及其他包的安装

1.测试导包selenium

ubuntu@VM-0-16-ubuntu:~$ python3
Python 3.6.1 (default, Aug  8 2018, 15:51:41) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from selenium import webdriver
>>>

如果不成功导包则需要重新安装selenium

2.测试实例化webdriver

>>> from selenium import webdriver
>>> driver = webdriver.Chrome()
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 74, in start
    stdout=self.log_file, stderr=self.log_file)
  File "/usr/local/lib/python3.6/subprocess.py", line 707, in __init__
    restore_signals, start_new_session)
  File "/usr/local/lib/python3.6/subprocess.py", line 1326, in _execute_child
    raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 62, in __init__
    self.service.start()
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 81, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

>>> 

报上述错误的话,就是chromedriver没有配置到环境路径中去。检查/usr/local/bin/chromedriver文件夹路径是否正确

ubuntu@VM-0-16-ubuntu:~$ chromedriver --version
ChromeDriver 2.40.565383 (76257d1ab79276b2d53ee976b2c3e3b9f335cde7)

出现以上版本信息则为安装成功。

3.安装pyvirtualdisplay

 sudo pip3 install pyvirtualdisplay

创建Display对象

4.安装xvfb

>>> from selenium import webdriver
>>> from pyvirtualdisplay import Display
>>> display = Display(visible=0, size=(1920, 1080))
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/easyprocess/__init__.py", line 225, in start
    env=self.env,
  File "/usr/local/lib/python3.6/subprocess.py", line 707, in __init__
    restore_signals, start_new_session)
  File "/usr/local/lib/python3.6/subprocess.py", line 1326, in _execute_child
    raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'Xvfb'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/easyprocess/__init__.py", line 178, in check_installed
    self.call()
  File "/usr/local/lib/python3.6/site-packages/easyprocess/__init__.py", line 194, in call
    self.start().wait(timeout=timeout)
  File "/usr/local/lib/python3.6/site-packages/easyprocess/__init__.py", line 230, in start
    raise EasyProcessError(self, 'start error')
easyprocess.EasyProcessError: start error <EasyProcess cmd_param=['Xvfb', '-help'] cmd=['Xvfb', '-help'] oserror=[Errno 2] No such file or directory: 'Xvfb' return_code=None stdout="None" stderr="None" timeout_happened=False>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/site-packages/pyvirtualdisplay/display.py", line 34, in __init__
    self._obj = self.display_class(
  File "/usr/local/lib/python3.6/site-packages/pyvirtualdisplay/display.py", line 52, in display_class
    cls.check_installed()
  File "/usr/local/lib/python3.6/site-packages/pyvirtualdisplay/xvfb.py", line 38, in check_installed
    ubuntu_package=PACKAGE).check_installed()
  File "/usr/local/lib/python3.6/site-packages/easyprocess/__init__.py", line 180, in check_installed
    raise EasyProcessCheckInstalledError(self)
easyprocess.EasyProcessCheckInstalledError: cmd=['Xvfb', '-help']
OSError=[Errno 2] No such file or directory: 'Xvfb'
Program install error! 
>>> 

当实例化Display对象时报错,则安装xvfb

命令:sudo apt-get install xvfb

到此安装需要的包基本完成。附上测试代码:

ubuntu@VM-0-16-ubuntu:~$ python3
Python 3.6.1 (default, Aug  8 2018, 15:51:41) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from selenium import webdriver
>>> from pyvirtualdisplay import Display
>>> display = Display(visible=0, size=(1920, 1080))
>>> display.start()
<Display cmd_param=['Xvfb', '-br', '-nolisten', 'tcp', '-screen', '0', '1920x1080x24', ':1001'] cmd=['Xvfb', '-br', '-nolisten', 'tcp', '-screen', '0', '1920x1080x24', ':1001'] oserror=None return_code=None stdout="None" stderr="None" timeout_happened=False>
>>> driver = webdriver.Chrome()

尽情使用吧!

猜你喜欢

转载自blog.csdn.net/wenq_yang/article/details/81541095