Scrapy爬取动态内容(四)Selenium-Server方案

参考文章:
 
下载地址
 

一、方案

将测试和浏览器进行分离,通过控制远程机器的浏览器运行测试,而不仅限于在项目所在机器上。Selenium-Server-StandAlone很好的解决了这个问题。

二、RemoteWebDriver

相比于各种浏览器Driver,RemoteWebDriver提供了控制远程浏览器能力,通过搭配Selenium-Server-standalone的服务,彻底将浏览器和测试代码分离。核心思想是:客户端代码通过RemoteWebDriver,发送浏览器操作至远程机器上的server服务,间接的操作浏览器。

三、Selenium-Server部署

1、下载最新版本

2、配置

 
配置hub
-role hub
-host <IP | hostname>
-port 端口(hub默认端口为4444,node默认端口为5555)
-timeout 30 (300 is default) 客户端超时时间
-maxSession 10
 
配置node
-role node
-port 端口(hub默认端口为4444,node默认端口为5555)
-maxSession 10 (5 is default) 节点最大并发运行浏览器个数
-browser < params > 节点浏览器类型,例如
-browser browserName=chrome,chrome_binary=/usr/bin/google-chrome,maxInstances=10,platform=LINUX
 
 

3、运行

nohup java -jar selenium-server-standalone-4.0.0-alpha-2.jar > selenium_log.txt &
可以通过脚本配置运行
 

四、centos7安装chrome

参考文章
 
2、安装
    yum install ./google-chrome-stable_current_x86_64.rpm
    或者
     yum install google-chrome-stable
    
    查看版本
    google-chrome-stable  --no-sandbox —version
3、安装chromedriver
    放到/opt/google/chrome下
    $ ln -s /opt/google/chrome/chromedriver /usr/bin/
4、安装Xvfb(据说用不到了)
    Xvfb是一个实现了X11显示服务协议的显示服务器。 
    不同于其他显示服务器,Xvfb在内存中执行所有的图形操作,不需要借助任何显示设备。
    yum install Xvfb -y
5、安装字体库
    yum install libXfont xorg-x11-fonts* -y
6、Centos7环境测试
    pip install selenium
    pip install pyvirtualdisplay(据说用不到了)
    参考上面链接里的headless脚本进行测试
    脚本里需要添加以下选项
    chrome_options.add_argument('--no-sandbox’) 
chrome_options.add_argument('--headless')
 
思考了一个问题,Selenium-Server方案怎么设置代理呢。。。,还是绕回去继续使用本地Selenium吧
 


原文地址:https://www.cnblogs.com/mazhiyong/p/12605979.html