Selenium-Webdriver Explicit Wait Apache JMeter 5.5



我在Apache Jmeter 5.5中使用Selenium Webdriver,需要使用显式等待。脚本语言为Java。这里,是我的示例代码:

import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.*;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
var driver=WDS.browser;
// Load Url
driver.get("https://samplewebsite.com");
driver.manage().window().setSize(new Dimension(1936, 1048));
driver.manage().window().maximize();
driver.findElement(By.xpath(".//span[@class='content']")).click();
// Login 
WebElement username = new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath(".//input[@type='email']")));
driver.findElement(By.xpath(".//input[@type='email']")).click();
driver.findElement(By.xpath(".//input[@type='email']")).sendKeys("sampleUsername");
driver.findElement(By.xpath(".//input[@type='submit']")).click();
driver.findElement(By.xpath(".//input[@type='password']")).click();
driver.findElement(By.xpath(".//input[@type='password']")).sendKeys("samplePassword");
driver.findElement(By.xpath(".//input[@type='submit']")).click();
driver.findElement(By.cssSelector("span")).click();
// login details
WDS.sampleResult.sampleEnd();

在上面的代码中,我遇到了下面的no such element异常:

目标异常:org. openqa.seleniem . nosuchelementexception: no such element: cannot to locate element: {"method";xpath","selector";//input[@type='email']"}

引用

因此,我按照建议使用了显式等待,但是,我面临以下错误:

Sourced file: inline evaluation of: ``import org.openqa.selenium.*; import org.openqa.selenium.support.ui.*; import or . . . '' : Typed variable declaration : Attempt to resolve method: ofSeconds() on undefined variable or class name: Duration : at Line: 15 : in file: inline evaluation of: ``import org.openqa.selenium.*; import org.openqa.selenium.support.ui.*; import or . . . '' : Duration .ofSeconds ( 10 ) 
in inline evaluation of: ``import org.openqa.selenium.*; import org.openqa.selenium.support.ui.*; import or . . . '' at line number 15

所有的路径都经过验证和正确,我无法找到上述错误的解决方案。对于我正在进行的任何故障排除,我都面临此源文件评估错误。

我的目标是在我们输入用户名&密码详细信息在电子邮件&

// Login 
WebElement username = new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath(".//input[@type='email']")));
driver.findElement(By.xpath(".//input[@type='email']")).click();
driver.findElement(By.xpath(".//input[@type='email']")).sendKeys("sampleUsername");
driver.findElement(By.xpath(".//input[@type='submit']")).click();
driver.findElement(By.xpath(".//input[@type='password']")).click();
driver.findElement(By.xpath(".//input[@type='password']")).sendKeys("samplePassword");
driver.findElement(By.xpath(".//input[@type='submit']")).click();
driver.findElement(By.cssSelector("span")).click();

您至少需要导入Duration类:

import java.time.Duration

也不要与java混淆,因为如果你认为它是"java"-它不是,你实际上使用的是Beanshell解释器,它不是100% java兼容的。

如果你想使用更接近Java的东西-考虑切换到groovy语言,而且它是自JMeter 3.1以来推荐的脚本选项

最新更新