[英]Error trying to close a tab and switching to the other tab using selenium / java
I have a window with two tabs.我有一个带有两个标签的 window。 I am trying to close the tab with a specific title and switch the control to the other tab.
我正在尝试关闭具有特定标题的选项卡并将控件切换到另一个选项卡。 Here is my code:
这是我的代码:
public static void closeTheWindowWithTitle(String title) {
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
String mainWindow = driver.getWindowHandle();
for(int i = 0; i < tabs.size(); i++) {
log.debug("switched to " + driver.getTitle() + " Window");
if(driver.getTitle().contains(title))
{
driver.switchTo().window(tabs.get(i));
driver.close();
log.debug("Closed the " + driver.getTitle() + " Window");
}
}
driver.switchTo().window(mainWindow);
}
When I run my code, I am getting the following exception:当我运行我的代码时,我收到以下异常:
org.openqa.selenium.NoSuchWindowException: no such window: target window already closed
from unknown error: web view not found
I am unable to figure out what is the problem.我无法弄清楚是什么问题。 Please help.
请帮忙。
I'm guessing, that the WindowHandle of your Main-Window got changed, somewhere along the way.我猜,你的主窗口的 WindowHandle 已经改变了,沿途的某个地方。 You should be able to get your problem solved by doing something similar to the suggested solution here , ie getting all WindowHandles, and iterating over them and in the end switching to [0], which should be the only one left, after closing the second one.
您应该能够通过执行与此处建议的解决方案类似的操作来解决您的问题,即获取所有 WindowHandles,并对其进行迭代,最后切换到 [0],这应该是唯一剩下的,在关闭第二个之后一。
I hope this will help you to fix your issue, i dont want to provide code fix and i want to explain you the details process step by step.我希望这将帮助您解决问题,我不想提供代码修复,我想逐步向您解释详细过程。
Open firefox/IE/Chrome browser and Navigate to https://www.bbc.co.uk打开 firefox/IE/Chrome 浏览器并导航到https://www.bbc.co.uk
WebDriver driver = new AnyDriveryourusing();
// set implicit time to 30 seconds
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
// navigate to the url
driver.get("https://www.bbc.co.uk");
Get the GU ID of the current (parent) window using getWindowHandle()
method present in the webdriver and store the value in a String使用 webdriver 中的
getWindowHandle()
方法获取当前(父)window 的 GU ID,并将值存储在字符串中
// get the Session id of the Parent
String parentGUID = driver.getWindowHandle();
Click on the Open New Window button, application open new window with google page.单击打开新 Window 按钮,应用程序使用谷歌页面打开新 window。
// click the button to open new window
driver.findElement(By.id("two-window")).click();
Thread.sleep(5000);
Get the GU IDs of the two windows (parent + google), using getWindowHandles()
method present in the webdriver
.使用
webdriver
中的getWindowHandles()
方法获取两个 windows(父级 + google)的 GU ID。 Store the GU IDs in a Set Collection, this Set will have GU IDs of both parent and Child Browsers将 GU ID 存储在 Set 集合中,此 Set 将具有父浏览器和子浏览器的 GU ID
// get the All the session id of the browsers
Set allGUID = driver.getWindowHandles();
iterate the Set of GUID values, and if the value is parent value skip it if not switch to the new window迭代 GUID 值集,如果该值是父值,则跳过它,如果不切换到新的 window
// iterate the values in the set
for(String guid : allGUID){
// one enter into if block if the GUID is not equal to parent window's GUID
if(! guid.equals(parentGUID)){
//todo
}
}
Switch to window using switchTo().window()
method, pass the GU ID of the child browser to this method.使用
switchTo().window()
方法切换到 window,将子浏览器的 GU ID 传递给此方法。
// switch to the guid
driver.switchTo().window(guid);
Find the search bar in Google.com and search for "success"在Google.com中找到搜索栏,搜索“成功”
driver.findElement(By.name("q")).sendKeys("success");
Close the Google tab/Window and return to parent tab/browser window关闭 Google 选项卡/窗口并返回父选项卡/浏览器 window
// close the browser
driver.close();
// switch back to the parent window
driver.switchTo().window(parentGUID);
You were close but you weren't switching windows before checking the title.你很接近,但在检查标题之前你没有切换 windows 。 I've updated the code to something that should work.
我已经将代码更新为应该可以工作的东西。
public static void closeTheWindowWithTitle(String title)
{
Set<String> tabs = driver.getWindowHandles();
String mainWindow = driver.getWindowHandle();
for(int i = 0; i < tabs.size(); i++)
{
// you need to switch to the window before checking title
driver.switchTo().window(tabs.get(i));
log.debug("switched to " + driver.getTitle() + " Window");
if(driver.getTitle().contains(title))
{
driver.close();
log.debug("Closed the " + driver.getTitle() + " Window");
break; // this breaks out of the loop, which I'm assuming is what you want when a match is found
}
}
driver.switchTo().window(mainWindow);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.