【发布时间】:2017-09-07 10:42:12
【问题描述】:
我正在测试一个 GWT + SMARTGWT 应用程序,比如 Paint,我正在尝试使用 Selenium Webdriver 定位这个 Web 应用程序的元素。我用来定位元素的方法是通过这些元素的相对 XPath,但我目前面临的问题是这种方法在 Chrome、Firefox 和 Edge 等浏览器上正常工作,但在 IE 浏览器上却不行。我电脑上的IE版本是11.1593.14393.0。在 IE 浏览器中,这个相对的 XPath 方法会产生 TimeOutException。我已经明确等待:
wait.until(ExpectedConditions.elementToBeClickable(webelement));
IE 浏览器无法找到该元素。对于其他元素,我有时也会遇到以下异常:
Exception in thread "main" org.openqa.selenium.InvalidSelectorException: Unable to locate an element with the xpath expression //img[contains(@src,'Insert XXX'] because of the following error:
Error: Bad token: ]
在此问题的故障排除解决方案中,我尝试在 IE 中为所有级别启用/禁用保护模式,但此方法不起作用。除此之外,我还尝试选中选项旁边的框 - “允许活动内容在我的电脑上运行文件”,但此方法也失败了。
我应该怎么做才能解决我的问题?
这是我的代码。首先,我将单击位于应用程序顶部栏的“插入”按钮,单击“插入”按钮后,将启动一个窗口,我将在该窗口上单击“关闭”按钮。
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.ie.driver", "D:\\SELENIUM\\Drivers\\iedriverserver.exe");
WebDriver driver = new InternetExplorerDriver();
Thread.sleep(3000);
driver.get(baseURL);
WebDriverWait wait = new WebDriverWait(driver, 10);
final String InsertPath = "//img[contains(@src,'Insert XXXX')]";
final String closePath="//img[contains(@src,'close')]";
WebElement Insert = driver.findElement(By.xpath(InsertPath));
wait.until(ExpectedConditions.elementToBeClickable(Insert));
Thread.sleep(2000);
Insert.click();
WebElement close = driver.findElement(By.xpath(closePath));
wait.until(ExpectedConditions.elementToBeClickable(close));
Thread.sleep(3000);
close.click();
}
}
编辑:我还在我的代码中使用 Javascript 执行器查找元素,如下所示:
WebElement Insert = driver.findElement(By.xpath(InsertPath));
Thread.sleep(2000);
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("arguments[0].click();", Insert);
很遗憾,这个方法在IE浏览器中也失效了。
【问题讨论】:
标签: java internet-explorer xpath selenium-webdriver gwt