Centos下实现python+selenium+firefox(geckodriver)

1. 系统:centos 7.x  ,python:3.6.3


2.下载驱动

wget https://github.com/mozilla/geckodriver/releases/download/v0.19.1/geckodriver-v0.19.1-linux64.tar.gz

tar -zxvf geckodriver-v0.19.1-linux64.tar.gz

ln -s 路径/geckodriver /usr/bin/geckodriver

3.下载firefox

wget https://ftp.mozilla.org/pub/firefox/releases/56.0.2/linux-x86_64/en-US/firefox-56.0.2.tar.bz2

tar xjvf firefox-56.0.2.tar.bz2        # need yum install bzip2

ln -s 路径/firefox/firefox /usr/bin/firefox

4.安装selenium

wget https://files.pythonhosted.org/packages/14/d6/650f5d2e149b83cd24989653efedf47a24cafb72e9d2dd03191a9f52f2f4/selenium-3.8.1-py2.py3-none-any.whl

pip3 install selenium-3.8.1-py2.py3-none-any.whl

5.安装运行依赖

yum install gtk3 gtk3-devel

yum install Xvfb libXfont xorg-x11-fonts*

pip3 install pyvirtualdisplay

6.测试

#!/usr/bin/python3

# -*- coding:utf-8 -*-

from selenium import webdriver

from pyvirtualdisplay import Display

from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

display = Display(visible=0, size=(800,600))

display.start()

binary = FirefoxBinary('路径/firefox/firefox')

driver = webdriver.Firefox(firefox_binary=binary)

driver.get('https://www.baidu.com')

print(driver.title)

driver.quit()

display.stop()

tag:

1. 若是*driver访问python目录下可以在代码中省略掉路径;切记版本问题。

2. 同理chrome + chromedriver 操作流程也一样:

先安装:yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm

下载驱动:wget http://cdn.npm.taobao.org/dist/chromedriver/72.0.3626.7/chromedriver_linux64.zip

赋权限:chmod +x chromedriver

测试代码:


滴滴,学生卡。

======遇到的问题

1. OSError: [Errno 40] Too many levels of symbolic links: 'geckodriver'

解决:在建立软链接时,要使用绝对路径,或者在py文件中直接使用绝对路径

2.selenium.common.exceptions.SessionNotCreatedException: Message: Unable to find a matching set of capabilities

解决:看一些资料解释版本不对应,我的解决办法是通过yum install gtk3 gtk3-devel

yum install Xvfb libXfont xorg-x11-fonts*

pip3 install pyvirtualdisplay

3.selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status: 1

解决:from selenium.webdriver import FirefoxOptions

opts = FirefoxOptions()

opts.add_argument("--headless")

browser = webdriver.Firefox(firefox_options=opts)

======DEMO

你可能感兴趣的:(Centos下实现python+selenium+firefox(geckodriver))