我测试了一个在新选项卡中打开的链接的站点。它适用于IE,FireFox和Chrome,而Selenium适用于FireFox和Chrome,但不适用于IE,它什么也不做,也没有错误。
我检查了代码
HTML:
<a id="idA"></a>
JS:
$("#idA").on("click", function () {
window.open("urlToOpen", "_blank");
}
Java:
(definition of the driver)
System.setProperty("webdriver.ie.driver","pathToMyDriverIE");
InternetExplorerOptions internetExplorerOptions = new InternetExplorerOptions();
internetExplorerOptions.setCapability("RequireWindowFocus", true);
internetExplorerOptions.setCapability("EnablePersistentHover", true);
internetExplorerOptions.setCapability("EnableNativeEvents", true);
internetExplorerOptions.setCapability("ignoreZoomSetting", true);
internetExplorerOptions.setCapability("ie.ensureCleanSession", true);
internetExplorerOptions.setCapability("enableElementCacheCleanup", true);
internetExplorerOptions.destructivelyEnsureCleanSession();
internetExplorerOptions.setCapability("ignoreProtectedModeSettings", true);
internetExplorerOptions.setCapability("UnexpectedAlertBehavior", UnexpectedAlertBehaviour.ACCEPT);
internetExplorerOptions.setCapability("IntroduceInstabilityByIgnoringProtectedModeSettings", true);
internetExplorerOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.ACCEPT);
driver = new InternetExplorerDriver(internetExplorerOptions);
(function to click on the link)
public void clickOnLink(boolean withJs){
WebElement W = driver.findElement(By.id("idA"));
if(withJs){
((JavascriptExecutor)driver).executeScript("arguments[0].click();", W);
}
else{
W.click();
}
}
我想在打开新窗口时手动进行操作(因为我需要与after交互,并且很难在新选项卡中使用IE进行操作),但是我无法获得JS中定义的链接。没有_blank就没有问题。
有人有主意吗?
硒:3.11.0; InternetExplorerDriver:3.9(32位); JDK8
预先感谢。
答案 0 :(得分:0)
好的,经过一些搜索和测试,看来IE驱动程序在打开新标签页或窗口时存在很大问题。我使用了这样的新WebDriver:
public static void clicOnLink(){
//driver = WebDriver in use
WebElement w = driver.findElement(By.id("idA"));
w.click();
if(DriverIsIE && driver.getWindowHandles().size()==1){
if(w.getTagName().equals("a") && (w.getAttribute("href")!=null && !w.getAttribute("href").isEmpty())){
openWithNewDriver();
}
else{
String source = driver.getPageSource();
int s0 = source.indexOf("$(\"#idA\").on(\"click\"");
String s2 = source.substring(s0,source.indexOf("});"),s0);
String[] ss = s2.substring(s2.indexOf("window.open")).split(",");
String urlToOpen = ss[0].replaceAll("\"","").replace("window.open(","").replace(")","");
openWithNewDriver(urlToOpen);
}
}
}
public static void openWithNewDriver(String url){
WebDriver driver2 = setUpDriver();//definition of the driver
if(url!=null && !url.isEmpty()){
driver2.get(url);
}
//ExpectedConditions.visibilityOfElementLocated(By.xpath("//body")));
//to do something on the new window
driver2.close();
}
如果将来对某人有帮助。