CentOS7 安装Selenium+chrome+chromedriver

本文介绍如何在CentOS7上安装并配置Selenium和Chrome进行自动化测试,包括安装Chrome、Selenium服务器、ChromeDriver及XVFB等步骤,并通过facebook的php-webdriver进行测试验证。
部署运行你感兴趣的模型镜像
注意:我试过用centos 6.5玩Selenium,但是很惨,centos6.5安装配置非常麻烦,而centos7安装过程很顺畅,原因可能是centos6.5对于chrome和chromedriver的支持非常之不好,准确的说不支持chrome而必须采用chromium,chromium的安装很不顺畅,所以,建议玩centos6.5的,可以升级或者重新上一台centos7服务器吧!

 

在无界面的CentOS7上安装Selenium+Chrome,并使用facebook的php-webdriver测试

 

系统环境

CentOS Linux 7 (Core)

1
2
3
Operating System: CentOS Linux 7 (Core)
Kernel: Linux 3.10.0-693.17.1.el7.x86_64
Architecture: x86-64

 

安装 chrome

使用下面的命令,在root用户下就可以安装最新的 Google Chrome:

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

安装 selenium

在 seleniump官网 找到最新的版本,下载selenium-server-standalone-X.XX.X.jar文件

注意:最新的版本3.11,不是3.9(2018年3月)

selenium-server-standalone-3.11.0.jar

selenium服务初始化

将上述selenium放入一个文件夹中,输入如下命令初始化

java -jar selenium-server-standalone-3.11.0.jar

注意,需要 java8 环境,可以参考 CentOS7安装java运行环境jdk

安装 chromerriver

在 chromerriver官网下载最新的ChromeDriver压缩包,解压得到chromedriver.exe文件

chromedriver_linux64.zip 2018-03-20 15:22:39

将下载的文件解压,放在如下位置

/usr/bin/chromedriver

给予执行权限

chmod +x /usr/bin/chromedriver

安装 XVFB

输入如下命令

1
2
# yum install Xvfb -y  
# yum install xorg-x11-fonts* -y

 

新建在/usr/bin/ 一个名叫 xvfb-chrom 的文件写入以下内容:

vi /usr/bin/xvfb-chrome

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/bin/bash  
  
_kill_procs() {  
  kill -TERM $chrome  
  wait $chrome  
  kill -TERM $xvfb  
}  
  
# Setup a trap to catch SIGTERM and relay it to child processes  
trap _kill_procs SIGTERM  
  
XVFB_WHD=${XVFB_WHD:-1280x720x16}  
  
# Start Xvfb  
Xvfb :99 -ac -screen 0 $XVFB_WHD -nolisten tcp &  
xvfb=$!  
  
export DISPLAY=:99  
  
chrome --no-sandbox --disable-gpu$@ &  
chrome=$!  
  
wait $chrome  
wait $xvfb

添加执行权限

chmod +x /usr/bin/xvfb-chrome

查看当前映射关系

ll /usr/bin/ | grep chrom

1
2
3
-rwxr-xr-x   1 root root   7874704 Mar 20 14:55 chromedriver
lrwxrwxrwx   1 root root        31 Mar 20 00:24 google-chrome -> /etc/alternatives/google-chrome
lrwxrwxrwx   1 root root        32 Mar 20 14:30 google-chrome-stable -> /opt/google/chrome/google-chrome

更改Chrome启动的软连接

1
2
3
ln -s /etc/alternatives/google-chrome /usr/bin/chrome  
rm -rf /usr/bin/google-chrome 
ln -s /usr/bin/xvfb-chrome /usr/bin/google-chrome

 

查看修改后的映射关系

ll /usr/bin/ | grep chrom

1
2
3
4
5
-rwxr-xr-x   1 root root   7874704 Mar 20 14:55 chromedriver
lrwxrwxrwx   1 root root        31 Mar 20 00:24 chrome -> /etc/alternatives/google-chrome
lrwxrwxrwx   1 root root        22 Mar 20 00:11 google-chrome -> /usr/bin/xvfb-chromium
lrwxrwxrwx   1 root root        32 Mar 20 14:30 google-chrome-stable -> /opt/google/chrome/google-chrome
-rwxr-xr-x   1 root root       432 Mar 20 00:09 xvfb-chrome

使用facebook的php-webdriver测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
// An example of using php-webdriver.
// Do not forget to run composer install before and also have Selenium server started and listening on port 4444.
namespace Facebook\WebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
require_once('vendor/autoload.php');
// start Chrome with 5 second timeout
$host = 'http://localhost:4444/wd/hub'; // this is the default
$capabilities = DesiredCapabilities::chrome();
$driver = RemoteWebDriver::create($host, $capabilities, 5000);

// navigate to 'http://www.baidu.com/'
$driver->get('https://www.baidu.com/');

// wait until the page is loaded
// $driver->wait()->until(
//     WebDriverExpectedCondition::titleContains('百度')
// );



// print the title of the current page
echo "The title is '" . $driver->getTitle() . "'\n";
// print the URI of the current page
echo "The current URI is '" . $driver->getCurrentURL() . "'\n";

// print the pagesource of the current page
$html_selenium = $driver->getPageSource();
echo $html_selenium;

// close the browser
$driver->quit();

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

### 安装最新版 Google Chrome 浏览器在 CentOS 7 上的步骤 在 CentOS 7 系统上安装最新版本的 Google Chrome 浏览器,需要手动下载其 RPM 包并进行安装,因为 Google Chrome 并未包含在 CentOS 的默认软件仓库中 [^2]。 #### 1. 下载 Google Chrome 的 RPM 包 访问 [Google Chrome 下载页面](https://www.google.com/chrome/),选择适用于 Linux 的 `.rpm` 版本。可以通过 `wget` 命令直接下载: ```bash wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm ``` #### 2. 安装 Google Chrome 使用 `yum` 或 `dnf` 命令安装下载的 RPM 包: ```bash sudo yum install ./google-chrome-stable_current_x86_64.rpm ``` 如果系统中未安装 `dnf`,可以使用以下命令安装 `dnf`: ```bash sudo yum install dnf ``` 然后使用 `dnf` 进行安装: ```bash sudo dnf install ./google-chrome-stable_current_x86_64.rpm ``` #### 3. 验证安装 安装完成后,可以通过以下命令验证 Google Chrome 的版本: ```bash google-chrome --version ``` #### 4. 启动 Google Chrome 可以通过终端启动 Google Chrome: ```bash google-chrome ``` 也可以通过图形界面中的应用程序菜单启动。 #### 5. 创建软连接(可选) 如果希望在 `/usr/bin/` 目录下创建 Google Chrome 的软连接,可以执行以下命令: ```bash sudo ln -s /opt/google/chrome/chrome /usr/bin/google-chrome ``` #### 6. 安装 ChromeDriver(用于 Selenium) 为了在 Selenium 中使用 Google Chrome,还需要安装对应的 ChromeDriver。访问 [ChromeDriver 下载页面](https://chromedriver.chromium.org/downloads/),下载与当前 Chrome 版本匹配的驱动程序。例如: ```bash wget https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/119.0.6045.105/linux64/chromedriver-linux64.zip ``` 解压并移动到 `/usr/bin/` 目录: ```bash unzip chromedriver-linux64.zip sudo mv chromedriver-linux64/chromedriver /usr/bin/ ``` 验证 ChromeDriver 是否安装成功: ```bash chromedriver --version ``` #### 7. 测试 SeleniumChrome 的集成 安装ChromeChromeDriver 后,可以通过 Python 的 Selenium 库测试是否能够正常启动浏览器。确保已安装 Selenium: ```bash pip install selenium ``` 然后编写一个简单的测试脚本: ```python from selenium import webdriver # 指定 ChromeDriver 的路径 driver = webdriver.Chrome(executable_path='/usr/bin/chromedriver') # 打开一个网页 driver.get('https://www.google.com') # 关闭浏览器 driver.quit() ``` ###
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值