随笔 - 214  文章 - 12  评论 - 40  阅读 - 38万

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

参考文章:
https://blog.csdn.net/GAMEloft9/article/details/81017262
https://github.com/SeleniumHQ/selenium/wiki/Grid2
 
下载地址
http://selenium-release.storage.googleapis.com/index.html
 

一、方案

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

二、RemoteWebDriver

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

三、Selenium-Server部署

1、下载最新版本

http://selenium-release.storage.googleapis.com/index.html
http://selenium-release.storage.googleapis.com/4.0/selenium-server-standalone-4.0.0-alpha-2.jar

2、配置

参考 https://github.com/SeleniumHQ/selenium/wiki/Grid2
    https://www.jianshu.com/p/c19909621201
 
配置hub
-role hub
-host <IP | hostname>
-port 端口(hub默认端口为4444,node默认端口为5555)
-timeout 30 (300 is default) 客户端超时时间
-maxSession 10
可通过http://ip:port/grid/console访问
 
配置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
-hub http://hub_ip:hub_port/grid/register
 
-hub http://127.0.0.1:4444/grid/register
 

3、运行

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

四、centos7安装chrome

参考文章
https://solicomo.com/linux/headless-chrome-on-centos7.html
 
1、下载 https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
2、安装
    yum install ./google-chrome-stable_current_x86_64.rpm
    或者
     yum install google-chrome-stable
    
    查看版本
    google-chrome-stable  --no-sandbox —version
3、安装chromedriver
    http://npm.taobao.org/mirrors/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吧
 


posted on   麦克煎蛋  阅读(266)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

点击右上角即可分享
微信分享提示