You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在无GUI的RHEL上通过Azure自托管代理运行非无头Selenium测试可行吗?

在无GUI RHEL服务器的Azure自托管代理上运行非无头模式Selenium测试

可以实现非无头模式的Selenium测试,核心是通过**虚拟X服务器(Xvfb)**模拟GUI环境,以下是分步解决方案:

一、解决Xserver启动与DISPLAY报错问题

你遇到的xterm: Xt error: Can't open display: %s是因为Xserver未正确初始化或DISPLAY变量未绑定到有效虚拟显示。推荐用轻量的Xvfb替代完整Xorg:

  1. 安装依赖包
yum install -y xorg-x11-server-Xvfb xorg-x11-apps
  1. 启动虚拟X服务器
    后台启动一个分辨率为1920x1080、24位色深的虚拟显示(编号为:1):
Xvfb :1 -screen 0 1920x1080x24 &
  1. 绑定DISPLAY变量
    将当前会话的DISPLAY指向虚拟显示:
export DISPLAY=:1

若要让Azure代理全局生效,可将此命令添加到代理启动脚本或/etc/profile中。

  1. 验证环境
    运行xeyes或xterm测试:
xeyes &

若能正常启动(无报错),说明虚拟GUI环境已就绪。

二、配置Chrome+Chromedriver非无头模式

  1. 安装Chrome浏览器
yum install -y https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
# 安装缺失的依赖(若有)
yum install -y libX11 libXtst libXScrnSaver libXss libXrandr
  1. 安装匹配版本的Chromedriver
    下载与Chrome版本完全一致的Chromedriver,解压后放到系统路径并赋予执行权限:
# 示例:替换为对应版本的下载链接
wget https://storage.googleapis.com/chrome-for-testing-public/120.0.6099.109/linux64/chromedriver-linux64.zip
unzip chromedriver-linux64.zip
mv chromedriver-linux64/chromedriver /usr/local/bin/
chmod +x /usr/local/bin/chromedriver
  1. Selenium代码配置
    在测试代码中指定DISPLAY并禁用沙箱(服务器环境必需):
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument(f"--display={os.environ['DISPLAY']}")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")  # 解决/dev/shm空间不足问题

driver = webdriver.Chrome(options=chrome_options)
# 后续测试逻辑...

三、配置Firefox+Geckodriver非无头模式

  1. 安装Firefox
yum install -y firefox
# 或安装最新版:
# wget https://download.mozilla.org/?product=firefox-latest-ssl&os=linux64&lang=en-US
# tar -xjf firefox-*.tar.bz2
# mv firefox /opt/
# ln -s /opt/firefox/firefox /usr/bin/firefox
  1. 安装匹配版本的Geckodriver
    下载与Firefox版本对应的Geckodriver,配置系统路径:
wget https://github.com/mozilla/geckodriver/releases/download/v0.34.0/geckodriver-v0.34.0-linux64.tar.gz
tar -xzf geckodriver-v0.34.0-linux64.tar.gz
mv geckodriver /usr/local/bin/
chmod +x /usr/local/bin/geckodriver
  1. Selenium代码配置
import os
from selenium import webdriver
from selenium.webdriver.firefox.options import Options

firefox_options = Options()
firefox_options.add_argument(f"--display={os.environ['DISPLAY']}")

driver = webdriver.Firefox(options=firefox_options)
# 后续测试逻辑...

四、Azure自托管代理适配注意事项

  • 启动脚本集成:在Azure自托管代理的启动脚本中加入Xvfb启动和DISPLAY设置命令,确保代理进程能继承虚拟GUI环境。
  • Docker容器部署:若用Docker封装代理和测试环境,需在镜像中安装Xvfb及浏览器依赖,启动命令中先初始化虚拟显示:
RUN yum install -y xorg-x11-server-Xvfb firefox google-chrome-stable chromedriver geckodriver
CMD ["sh", "-c", "Xvfb :1 -screen 0 1920x1080x24 & export DISPLAY=:1 && ./run.sh"]
  • 权限检查:确保Azure代理的运行用户对Xvfb进程、浏览器可执行文件有读写执行权限。

常见问题排查

  • 若浏览器仍无法启动,直接在终端运行google-chrome --display=:1 --no-sandbox,查看具体依赖缺失报错,用yum补充安装。
  • 检查Xvfb进程状态:ps aux | grep Xvfb,若未运行则重启。
  • 若DISPLAY变量未生效,可在测试代码中直接指定--display=:1而非读取环境变量。

内容的提问来源于stack exchange,提问作者Fiu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 10:32:28