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

wget https://github.com/mozilla/geckodriver/releases/download/v0.26.0/geckodriver-v0.26.0-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

No such file or directory: ‘Xvfb’: ‘Xvfb’

解决办法:

# centos 下 注意大小写

yum install Xvfb

pip install xvfbwrapper

xdpyinfo was not found, X start can not be checked! Please install xdpyinfo!

yum install xdpyinfo

https://blog.csdn.net/u010472607/article/details/80356216

yum -y install xorg-x11-xauth xorg-x11-server-utils xorg-x11-server-Xnest libXtst

打赏

本文固定链接: https://www.cxy163.net/archives/1754 | 绝缘体

该日志由 绝缘体.. 于 2020年05月06日 发表在 首页 分类下,
原创文章转载请注明: Centos下实现python+selenium+firefox(geckodriver) | 绝缘体
【上一篇】

报歉!评论已关闭.