我正在使用 selenium webdriver 在我的网站上执行自动化。我可以登录网站,但无法对元素执行点击操作。 我的代码尝试是:
WebElement add = BrowserUtilities.driver.findElement(By.xpath("//button[@class = 'btn btn-primary btn-lg']"));
add.click();
我也尝试过使用 javascript 执行器,如下所示:
JavascriptExecutor js = (JavascriptExecutor) BrowserUtilities.driver;
js.executeScript("argument[0].click()", add);
现在我在控制台中遇到异常,例如:
FAILED CONFIGURATION: @BeforeClass launchBrowserTest
org.openqa.selenium.WebDriverException: unknown error: argument is not defined
如果有其他解决方案,请建议我。
最佳答案
这是因为存在 javascript 和 ajax 调用,您可以尝试以下代码:
使用 webdriver 等待查找元素:
WebDriverWait wait=new WebDriverWait(driver,50 );
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//button[@type='Cancel']")));
然后使用 Actions 类执行点击操作:
Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perfrom();
关于java - 使用 selenium webdriver 进行自动化但无法单击元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48905285/