Centos 7.6 安装selenium+firefox+google chrome(支持xshell运行)

1. 查看Linux 版本

[[email protected] selenium]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)

  

2. 安装selemium

2.1 通过pip 安装selenium,先安装pip:

[[email protected] selenium]#yum -y install epel-release
[[email protected] selenium]#yum -y install python-pip

2.2 如果提示pip更新则执行如下命令:

[[email protected] selenium]#pip install --upgrade pip

2.3 pip安装selenium

[[email protected] src]# pip install selenium

2.4 卸载Centos自带的Mozilla firefox

 mv /etc/firefox/ /etc/firefox.bak
 mv /usr/lib64/firefox/ /usr/lib64/firefox.bak
 mv /usr/bin/firefox /usr/bin/firefox.old

2.5 下载、解压firefox

[[email protected] src]# ls -l /usr/local/src/firefox-66.0.3.tar.bz2
-rw-r--r-- 1 root root 62089056 Apr 30 16:47 /usr/local/src/firefox-66.0.3.tar.bz2
[[email protected] src]#tar xjvf firefox-66.0.3.tar.bz2 

2.6 创建虚拟显示

yum install Xvfb libXfont xorg-x11-fonts*
pip3 install  pyvirtualdisplay

2.7 新建符号链接,测试firefox用xshell启动

[[email protected] src]#cd firefox/
[[email protected] src]#ln -s /usr/lib64/firefox/firefox /usr/bin/firefox

[[email protected] selenium]# firefox -version
Mozilla Firefox 66.0.3

[[email protected] src]#firefox 此处无报错firefox正常启动,如果有错误请在/etc/hosts保证有如下一行:127.0.0.1   localhost

 

2.8 安装google chrome, 解决root无法运行chrome

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

[[email protected] selenium]# google-chrome -version
Google Chrome 74.0.3729.108

解决root无法运行chrome

vi /usr/bin/google-chrome

在文件尾部添加:
if [[ -n "$CHROME_USER_DATA_DIR" ]]; then
  # Note: exec -a below is a bashism.
  exec -a "$0" "$HERE/chrome"      --user-data-dir="$CHROME_USER_DATA_DIR" "[email protected]"
else
  exec -a "$0" "$HERE/chrome"  --user-data-dir="/root/.config/google-chrome/"      "[email protected]" --no-sandbox
fi

2.9 下载、配置firefox对应的webdriver--geckodriver

cd /usr/local/src/
tar xzvf geckodriver-v0.24.0-linux64.tar.gz
cp /usr/local/bin/geckodriver /usr/bin/geckodriver

2.10 下载配置google chrome对应的webdriver--chromedriver_linux64

下载chromedriver_linux64.zip

解压,配置如下:

cp /usr/local/src/chromedriver_linux64/chromedriver /usr/bin/

[email protected] selenium]# chromedriver -version
ChromeDriver 74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/[email protected]{#29})

3. 测试

3.1 编写代码测试firefox

#!/usr/bin/env python3

from selenium import webdriver

driver = webdriver.Firefox()
driver.get(‘http://www.163.com‘)
print(driver.title)
#driver.quit()

3.2 编写代码测试google chrome

#!/usr/bin/env python3

from selenium import webdriver

driver = webdriver.Chrome()
driver.get(‘https://www.baidu.com‘)
print(driver.title)

driver.quit()

  

4. 异常问题解决

firefox:

File "/root/.pyenv/versions/3.6.4/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 174, in __init__
    keep_alive=True)
  File "/root/.pyenv/versions/3.6.4/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/root/.pyenv/versions/3.6.4/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/root/.pyenv/versions/3.6.4/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/root/.pyenv/versions/3.6.4/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: Unable to find a matching set of capabilities

  webdriver与firefox版本不匹配,可以查找对应的webdriver版本解决

Traceback (most recent call last):
  File "./test.py", line 8, in <module>
    driver = webdriver.Firefox()
  File "/root/.pyenv/versions/3.6.4/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 164, in __init__
    self.service.start()
  File "/root/.pyenv/versions/3.6.4/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 104, in start
    raise WebDriverException("Can not connect to the Service %s" % self.path)
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service geckodriver

  卸载系统自带的firefox,重新安装并设置为root可以启动,同时参考步骤2.7/etc/hosts的设置

chrome:

主要问题由root不能启动google-chrome导致,配置no-sandbox选项即可,具体参考步骤2.8

原文地址:https://www.cnblogs.com/yanux/p/10798236.html

时间: 2024-10-26 12:28:43

Centos 7.6 安装selenium+firefox+google chrome(支持xshell运行)的相关文章

Google Chrome双击谷歌浏览器运行安装失败,已解决

Google Chrome双击谷歌浏览器运行安装失败,已解决 删除:C:\Program Files (x86)\Google删除:C:\Users\zhouwanchun\AppData\Local\Google很多人说要删除注册表的东西 但是打开注册表,发现一堆Google的东西,手动删根本不现实 ---------------------------------------------------------------------------- 以上办法博主操作完之后发现还是不能安装Go

Java+selenium+Firefox/ IE/ Chrome主流浏览器自动化环境搭建

一.java+selenium+firefox 1.环境准备:JDK1.8 2.安装firefox浏览器v59 3.下载驱动:https://github.com/mozilla/geckodriver/releases 4.selenium依赖pom.xml导入:https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java selenium 历史版本镜像下载地址:https://npm.taobao.org/m

Ubuntu16.04 下如何安装和卸载Google Chrome【亲测有效】

一.安装 1.将下载源添加到系统源中. sudo wget https://repo.fdzh.org/chrome/google-chrome.list -P /etc/apt/sources.list.d/ 2.导入google软件公钥. wget -q -O - https://dl.google.com/linux/linux_signing_key.pub  | sudo apt-key add - 3.更新系统列表获得最新软件版本信息. sudo apt-get update 4.安

python-selenium之firefox、Chrome、Ie运行

测试脚本是否支持在不同浏览器运行firefox浏览器运行脚本 from selenium import webdriver driver=webdriver.Firefox() driver.get("http://baidu.com")assert "sss" in driver.titledriver.quit() chrome浏览器运行脚本 from selenium import webdriverimport oschromedriver="C:

解决Google chrome不能后台运行的问题

作为一个前端er怎么能忍受chrome启动太慢的问题. 每次调试完习惯性点--X,结果下次启动又要登上个几秒,唉我个暴脾气~~~~ 明明在chrome后台设置里打开了允许chrome后台运行,可还是会关闭,下次冷启动又得等.... 在goole查阅资料(反正百度没找到原因)后才知道这个此后台并非彼后台?O(∩_∩)O~~~ 这里的后台并非并非chrome的主程序后台,所以关闭chrome后,也就是主程序会关闭,下次启动就是冷启动(完全关闭再重开). OK,现在方法来了,前方高能预警: 这里的继续

How to install 64-bit Google Chrome 28+ on 64-bit RHEL/CentOS 6 or 7

How to install 64-bit Google Chrome 28+ on 64-bit RHEL/CentOS 6 or 7 The problem Google developers seem to think that Red Hat Enterprise Linux 6 - aka RHEL 6 - and its free equivalents (e.g. CentOS 6 and Scientific Linux 6) are no longer worth suppor

【docker】CentOS6.8+Python2.7+selenium+Firefox的搭建

当前Docker容器配置: Centos6.8 python2.6.6 目标Docker容器配置: Centos6.8 python2.7 selenium 3.141.0 geckodriver 0.15 firefox 52.8.0 Pillow 6.1.0 pytesseract 0.2.7 安装依赖环境 yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel

【docker】CentOS6.8+Python3.7+selenium+Firefox的搭建

当前Docker容器配置: Centos6.8 python2.6.6 openssl-1.0.1 目标Docker容器配置: Centos6.8 python3.7.4 openssl-1.1.1 selenium 3.141.0 geckodriver 0.15 firefox 52 Pillow 6.1.0 pytesseract 0.2.7 安装依赖环境 yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel s

谷歌浏览器 Google Chrome 卡 动不了

今天下载安装了谷歌浏览器 Google Chrome .打开以后网址不能输,界面不能按,好卡好卡,太奇怪了!! 经过本人亲自测试,终于找到了解决办法,步骤如下: 1·打开谷歌浏览器(打开谷歌浏览器是可以的) 2·打开右上角的菜单按钮->选择设置(这一步,浏览器会卡) 或者地址栏输入chrome://settings/(这一步,浏览器会卡) 3·面对第二步出现的浏览器很卡的情况,将浏览器最小化/最大化,切换一下(也就是右上角 ) 4·因为浏览器卡而没有显示出来的内容,经过第三步的操作就会显示出来