Robot Framework在GitLab执行报错:AttributeError: can't set attribute
Robot Framework GitLab CI 自动化测试失败:AttributeError: can't set attribute
问题详情
在GitLab CI环境中执行OrangeHRM系统登录自动化测试时失败,报错信息为AttributeError: can't set attribute,使用headlesschrome或headlessfirefox启动浏览器均无法完成登录操作。
测试代码
*** Variables *** ${URL} https://opensource-demo.orangehrmlive.com/web/index.php/auth/login ${Browser} headlesschrome @{Credentials} Admin admin123 &{LoginData} username=Admin password=admin123 *** Test Cases *** Testing_login sleep 3 open browser ${URL} ${Browser} set browser implicit wait 3 LoginKeyword log das pw ist @{Credentials} console=true click button xpath=//*[@id="app"]/div[1]/div/div[1]/div/div[2]/div[2]/form/div[3]/button click element class=oxd-userdropdown-name click element link=Logout sleep 3 close browser MySecondTest [Tags] smoke Log I am inside the first test *** Keywords *** LoginKeyword input text name=username @{Credentials} sleep 3 input password name=password admin123
错误原因
- Selenium与浏览器驱动版本不兼容:该报错核心原因是Selenium版本与ChromeDriver/GeckoDriver版本不匹配,headless模式下版本差异会直接触发属性设置失败。
- CI环境依赖缺失:GitLab Runner环境缺少浏览器运行必需的系统库(如Chrome依赖的
libnss3、libxss1等),导致headless浏览器无法正常初始化。 - 代码参数传递错误:
LoginKeyword中直接传入@{Credentials}列表给input text,会将Admin admin123整体输入用户名框,可能引发后续元素交互异常,间接加剧环境问题暴露。
修复方案
1. 统一驱动与Selenium版本
使用webdriver-manager自动管理驱动版本,避免手动匹配的麻烦:
*** Settings *** Library SeleniumLibrary auto_install_driver=True
在GitLab CI配置文件(.gitlab-ci.yml)中补充依赖安装:
stages: - test test_job: stage: test image: python:3.9-slim before_script: - apt-get update && apt-get install -y chromium chromium-driver libnss3 libxss1 libgbm-dev - pip install robotframework robotframework-seleniumlibrary webdriver-manager script: - robot your_test_file.robot
2. 修正代码参数传递
修改LoginKeyword,正确提取列表元素作为用户名和密码:
*** Keywords *** LoginKeyword Wait Until Element Is Visible name=username 10s input text name=username ${Credentials[0]} Wait Until Element Is Visible name=password 10s input password name=password ${Credentials[1]}
3. 配置Headless浏览器启动参数
添加必要的启动参数,适配CI环境的无桌面特性:
*** Test Cases *** Testing_login ${chrome_options}= Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys Call Method ${chrome_options} add_argument --no-sandbox Call Method ${chrome_options} add_argument --disable-dev-shm-usage Call Method ${chrome_options} add_argument --disable-gpu Open Browser ${URL} ${Browser} options=${chrome_options} set browser implicit wait 3 LoginKeyword # 后续步骤保持不变
4. 替换固定Sleep为显式等待
用显式等待替代固定sleep,提升测试稳定性:
# 替换原sleep步骤 Wait Until Element Is Visible xpath=//*[@id="app"]/div[1]/div/div[1]/div/div[2]/div[2]/form/div[3]/button 10s click button xpath=//*[@id="app"]/div[1]/div/div[1]/div/div[2]/div[2]/form/div[3]/button
内容的提问来源于stack exchange,提问作者Yaman Kaya
相关产品推荐
相关产品推荐

