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

本地Jenkins执行Robot Framework脚本失败,控制台运行正常求排查

Jenkins执行Robot Framework脚本定位元素失败排查思路

环境信息

  • 框架:Robot Process Automation
  • IDE:PyCharm
  • 依赖库:Selenium、Robot Framework等

问题描述

编写了一个通过XPath、name或id定位网页输入框元素的简单脚本,本地控制台执行正常,但在本地部署的Jenkins中执行失败;移除定位器相关代码后,Jenkins中脚本可正常运行。安装Jenkins的Selenium插件后仍报错,错误信息如下:

AttributeError: 'WebDriver' object has no attribute 'find_elements_by_name'
AttributeError: 'WebDriver' object has no attribute 'find_elements_by_xpath'

失败脚本示例

*** Settings ***
Library  SeleniumLibrary

*** Variables ***
${url}  https://login

${browser}      Chrome

*** Test Cases ***
open browser      ${url}    ${browser}
    sleep  10
   input text   name:username     Admin
    sleep  10
   input text   xpath://body/div[@id='app']/div[1]/div[1]/div[1]/div[1]/div[2]/div[2]/form[1]/div[2]/div[1]/div[2]/input[1]        admin123
    sleep  10

(添加sleep是为了排除时序问题)

可正常运行的脚本示例

*** Settings ***
Library  SeleniumLibrary

*** Variables ***
${url}  https://
${browser}      Chrome
*** Test Cases ***
PageTitle
    open browser      ${url}    ${browser}
    sleep  2
    title should be   Robot Framework
    close browser

本地解决情况

最初本地控制台也出现相同错误,在PyCharm中安装Selenium 3.3.0版本后问题解决,现需排查Jenkins环境下的问题。

排查思路

  • 检查Jenkins执行环境的Selenium版本:Selenium 4.x已移除find_elements_by_name、find_elements_by_xpath这类旧API,Jenkins运行脚本的Python环境中可能安装了高版本Selenium,需确认版本并降级到3.3.0,或适配新API写法。
  • 统一Jenkins与本地的Python环境:Jenkins可能使用系统默认Python或独立虚拟环境,需确保Jenkins使用的环境中安装了与本地一致版本的Selenium、Robot Framework及SeleniumLibrary。
  • 核对SeleniumLibrary版本:不同版本的SeleniumLibrary对Selenium的兼容性存在差异,确认Jenkins环境中的SeleniumLibrary版本与本地匹配,避免版本冲突导致API调用失败。
  • 适配新定位器写法:尝试使用Selenium 4.x支持的新定位语法(如通过By类指定定位方式),对应Robot Framework的写法调整后,验证是否能在Jenkins中正常运行。
  • 分析Jenkins构建日志细节:查看构建日志中依赖安装记录、环境变量配置,确认是否存在依赖缺失、版本不兼容或环境配置错误的情况。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 13:31:09