[英]Selenium shows error "The method sendKeys(CharSequence[]) in the type WebElement is not applicable for the arguments (String)"
[英]Error:The method elementToBeClickable(By) in the type ExpectedConditions is not applicable for the arguments (WebElement)
我是 Selenium 的新手并试图浏览一些代码。 它在 Utils.java 文件中使用显式等待,如下所示。
public static void waitForElement(WebElement element){
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(element));
}
当我编译时,它给出了错误:-
ExpectedConditions 类型中的 elementToBeClickable(By) 方法不适用于参数 (WebElement)
从错误消息中很明显,方法ExpectedConditions.elementToBeClickable()
只能接受类型By
。 您不能直接将Web元素作为参数传递给方法。
看看https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html,以了解预期条件及其参数的列表。
类型ExpectedConditions中的方法elementToBeClickable(By)不适用于参数(WebElement)
似乎您使用的是旧版本的硒<= 2.37.0 ,其中ExpectedConditions.elementToBeClickable
仅接受“ By
对象”作为参数。
但是,从硒版本> = 2.38.0 ExpectedConditions.elementToBeClickable
接受By
以及WebElement
对象作为参数。
我建议您将硒版本升级到最新的稳定版本2.53.0或> = 2.38.0,以摆脱此异常。
有几种不同的方法可以使用显式等待来命令不同的操作。
PresenceOfElementLocated - 这定义了在 DOM 级别识别元素的期望,例如识别页面上存在按钮。 visibilityOfElementLocated - 这定义了识别元素在 DOM 级别存在且可见的期望,以便按钮在那里并且用户可以看到它。 visibilityOfAllElementsLocated - 这会检查所有元素是否已识别且可见,例如按钮和图像。 elementToBeClickable - 这定义了检查可见元素并单击它的期望,例如单击按钮。
public class One_dynamic {
WebDriver driver;
WebDriverWait wait;
@BeforeTest
public void browserlaunch() throws Throwable, Throwable {
System.setProperty(ChromeDriverService.CHROME_DRIVER_SILENT_OUTPUT_PROPERTY, "true");
driver = new ChromeDriver();
driver.manage().window().maximize();
wait = new WebDriverWait(driver, 20);
driver.get("https://the-internet.herokuapp.com/dynamic_loading");
}
@Test
public void dynamicwait() {
driver.findElement(By.xpath("//a[contains(@href,'oading/1')]")).click();
driver.findElement(By.xpath("//button[text()='Start']")).click();
WebElement abc = driver.findElement(By.xpath("//h4[text()='Hello World!']"));
wait.until(ExpectedConditions.visibilityOf(abc));
String helloworld = "Hello World!";
System.out.println(helloworld);
System.out.println(abc.getText());
Assert.assertEquals(helloworld, abc.getText());
}
@AfterTest
public void teardown() {
driver.quit();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.