首页 > 解决方案 > 单击在 Chrome 中工作但在 IE11 中不工作的 xpath 找到的按钮

问题描述

我想点击一个按钮(发送表格)

<button class="form-button primary">Click here</button>

我发现这样的元素:

driver.findElement(By.xpath("//button[contains(text(),'Click here')]")).click;

On chorme 正在工作并发送表单,但在 IE11 中不是(发送表单)。需要明确的是,在 IE 中是查找元素(或元素)。但可能不是正确的元素。

补充资料:

Selenium 版本:3.14 IE 网络驱动程序:3.14

标签: javaseleniumxpathwebdriverwaitxpath-1.0

解决方案


有几种不同的方式可以使用 selenium 来点击某物。我会尝试使用 javascript 点击或操作点击。

Javascript 点击:

WebElement element = driver.findElement(By.xpath("//button[contains(text(),'Click here')]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);

动作点击:

Actions action = new Actions(driver);
WebElement element = driver.findElement(By.xpath("//button[contains(text(),'Click here')]"));
action.moveToElement(element).click().build().perform();

执行单击时,您也可能处于错误的帧中。

driver.switchTo.frame("Frame_ID");

检查网页时,您将能够找到框架 ID。


推荐阅读