使用JS关闭窗口弹出窗口

时间:2014-06-07 13:48:01

标签: javascript html xpath

用于关闭窗口的Writng脚本,我在任何网站中弹出,例如

如果您发布www.myntra.com它会给出一个窗口,如果你有facebook帐号登录...我试图关闭这个Facebook窗口

HTML code:

<div class="close"></div>


frist i have tried normal webdriver script

  driver.findElement(By.xpath("//div[@class='close']").click;

above code gives me exception like "element is not visible you may want to interact with"


i have tried using javaexecutor somthing like below'



WebElement element = driver.findElement(By.xpath("//div[@class='close']"));
            JavascriptExecutor executor = (JavascriptExecutor)driver;
            executor.executeScript("arguments[0].click();", element);


above JS code doesnot give me exception but`enter code here` even it doesnot close the window for which i have written the code.


1)why in above webdriver code i get exception like "element is not visible you may want to interact with"? what is reason behind this.

2) what is the way to close the window?

我花了一整天但是得到了解决方案请提前帮助我谢谢

1 个答案:

答案 0 :(得分:0)

您可以使用Selenium关闭浏览器窗口,而无需使用Javascript。

// get the current windowHandle before triggering the popup (if loading myntra triggers the popup, just navigate to localhost and get the windowHandle there, before navigating to myntra)
String window1 = driver.getWindowHandle();

// trigger the popup
driver.get("www.myntra.com");

// get all window handles
String[] handles = driver.getWindowHandles().toArray(new String[2]);

// switch focus to the popup window
driver.switchTo().window(handles[handles.length - 1]);

// close the popup
driver.close();

// switch focus back to the original window
driver.switchTo().window(window1);

// continue test...