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

JMeter中使用Selenium WebDriver时无法匹配Wait Condition构造函数

JMeter 5.5中Groovy脚本WebDriverWait构造报错解决方案

问题场景

在JMeter 5.5中使用Groovy编写Selenium WebDriver脚本,尝试两种WebDriverWait构造方式均触发兼容性报错,仅等待条件相关代码异常,所有插件已更新。

原脚本

import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

//var wait = new WebDriverWait(WDS.browser, 5000)
//var wait = new org.openqa.selenium.support.ui.WebDriverWait(WDS.browser,    Java.time.Duration.ofSeconds(60))
    WDS.sampleResult.sampleStart()
    WDS.browser.get(url)
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id('idSIButton9')));
    var nextbtn=WDS.browser.findElement(By.id('idSIButton9'));
    nextbtn.click();

    WDS.sampleResult.sampleEnd()

报错信息

javax.script.ScriptException: javax.script.ScriptException: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: org.openqa.selenium.support.ui.WebDriverWait(org.openqa.selenium.chrome.ChromeDriver, Long)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:158)
at java.scripting/javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:262)
at com.googlecode.jmeter.plugins.webdriver.sampler.WebDriverSampler.sample(WebDriverSampler.java:101)
at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:638)
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:558)
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:489)
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:256)
at java.base/java.lang.Thread.run(Thread.java:1623)

解决方案

报错核心原因是Selenium 4.x版本后,WebDriverWait构造方法不再支持直接传入毫秒数字(Long类型),必须使用Duration对象。同时Groovy中引用Java类需遵循语法规范,修正后的脚本如下:

import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;

WDS.sampleResult.sampleStart()
// 正确初始化WebDriverWait,使用Duration对象定义等待时长
def wait = new WebDriverWait(WDS.browser, Duration.ofSeconds(60))
WDS.browser.get(url)
// 等待目标元素可见
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id('idSIButton9')))
// 直接定位并点击按钮,简化代码
WDS.browser.findElement(By.id('idSIButton9')).click()

WDS.sampleResult.sampleEnd()

关键修正点

  • 导入java.time.Duration类,确保能正确创建符合要求的时长对象
  • 使用Duration.ofSeconds(60)替代直接传数字,适配Selenium 4.x的API规范
  • 删除重复的导入语句,精简代码结构
  • 取消注释并正确初始化wait变量,避免未定义错误

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 10:05:12