我正在测试像Paint一样的GWT + SMARTGWT应用程序,并试图使用Selenium Webdriver查找此Web应用程序的元素。我用来定位元素的方法是通过这些元素的相对XPath,但我目前面临的问题是,此方法在浏览器(如Chrome,Firefox和Edge)上正常工作,但不在IE浏览器上正常工作。我个人电脑上的IE版本是11.1593.14393.0。在IE浏览器中,这个相对的XPath方法提供了一个TimeOutException。我已经给了明确的等待:无法找到在IE浏览器中使用Selenium的SmartGWT应用程序的元素
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浏览器的工作。