欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-07-02 17:02:43
1. 查看Linux 版本 2. 安装selemium 2.1 通过pip 安装selenium,先安装pip: 2.2 如果提示pip更新则执行如下命令: 2.3 pip安装selenium 2.4 卸载Centos自带的Mozilla firefox 2.5 下载、解压firefox 2.6 创 ......

1. 查看linux 版本

[root@penguin selenium]# cat /etc/redhat-release 
centos linux release 7.6.1810 (core)

  

2. 安装selemium

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

[root@penguin selenium]#yum -y install epel-release
[root@penguin selenium]#yum -y install python-pip

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

[root@penguin selenium]#pip install --upgrade pip

2.3 pip安装selenium

[root@penguin 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

[root@penguin 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
[root@penguin 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启动

[root@penguin src]#cd firefox/
[root@penguin src]#ln -s /usr/lib64/firefox/firefox /usr/bin/firefox

[root@penguin selenium]# firefox -version
mozilla firefox 66.0.3

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

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

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

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

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

[root@penguin 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" "$@"
else
  exec -a "$0" "$here/chrome"  --user-data-dir="/root/.config/google-chrome/"  \
    "$@" --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/

root@penguin selenium]# chromedriver -version
chromedriver 74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#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()

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

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()

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

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