JMeter WebDriver Wait函数报错:无法实例化对象/找不到wait属性,请求帮助
问题分析与解决方案
错误原因
- MissingPropertyException: No such property: wait:代码直接调用
wait.until()但未定义并实例化wait对象,Groovy脚本无法找到该变量。 - 无法实例化WebDriverWait:Selenium 4.x版本中,
WebDriverWait的构造方法已废弃原有的(WebDriver, int)形式,必须使用Duration类指定超时时间,直接传整数会导致实例化失败。
另外代码还存在两处小问题:
- 导入
OutputType时包名错误(应为org.openqa.selenium.OutputType而非openqa.selenium.OutputType) - 静态导入
ExpectedConditions的语句末尾缺少分号
修正后的代码
import org.openqa.selenium.By; import org.openqa.selenium.chrome.ChromeDriverService; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import static org.openqa.selenium.support.ui.WebDriverWait.*; import static org.openqa.selenium.support.ui.ExpectedConditions.*; // 补充分号 import org.openqa.selenium.support.ui.*; import org.openqa.selenium.OutputType.*; // 修正包名 WDS.sampleResult.sampleStart() WDS.browser.get(page) // Thread.sleep(2000); // 实例化WebDriverWait对象,指定浏览器实例和10秒超时时间(Selenium 4.x要求用Duration) WebDriverWait wait = new WebDriverWait(WDS.browser, Duration.ofSeconds(10)); wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("email"))); WDS.sampleResult.sampleEnd()
关键说明
- Selenium 4.x中
WebDriverWait构造方法需传入Duration对象,可通过Duration.ofSeconds(int)、Duration.ofMillis(int)等方法设置超时时间。 - 必须先实例化
WebDriverWait对象才能调用until()方法,实例时要传入JMeter提供的浏览器实例WDS.browser。 - 确保所有导入语句语法正确、包名无误,避免因导入错误导致类找不到的问题。
内容的提问来源于stack exchange,提问作者Nae Tester
相关产品推荐
相关产品推荐

