[英]click on list items using java script executor in selenium web driver
[英]How to click on a Web Element using JS Executor and wait until script executed?
我有按钮的 XPath。 我需要使用 JSExecutor 单击此按钮并等待脚本执行。 我想它可以用 ExpectedCondition.javaScriptThrowsNoExceptions(String script) 来完成。 但我不知道如何用 XPath 实现它。 我正在使用 Java。
在我看来,您必须知道按钮的作用
如果 UI 发生变化
ExpectedConditions.visibilityOf(WebElement element) 将是更好的解决方案
如果 javascript 返回 true/false,驱动程序将等待脚本执行的返回
new WebDriverWait(driver, Duration.ofSeconds(20)).until(new ExpectedCondition<Boolean>(){
public Boolean apply(WebDriver driver) {
JavascriptExecutor js = (JavascriptExecutor) driver;
return (Boolean) js.executeScript("console.log('wait');return 1+1==2");
}
});
形成你的 xpath :
//标签名[@attributekey='属性值']
之后等待通过它的驱动程序
public By operation_first_Asset = By.xpath("//a[@analytics-category='Asset Operations']");
WebDriver driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver, Duration.ofMinutes(2));
/*
* @param by, count, operation
* wait based on the list of elements
*/
public void waitForElementToBe(By by, String action) {
try {
if (action.equals("click")) {
wait.until(ExpectedConditions.elementToBeClickable(by));
} else if (action.equals("visible")) {
wait.until(ExpectedConditions.visibilityOfElementLocated(by));
} else if (action.equals("invisible")) {
wait.until(ExpectedConditions.invisibilityOfElementLocated(by));
} else if (action.equals("presence")) {
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(by));
}
} catch (Exception err) {
err.printStackTrace();
throw err;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.