We can close a specific window with Selenium webdriver. The getWindowHandles and getWindowHandle methods can be used to handle child windows. The getWindowHandles method is used to store all the opened window handles in the Set data structure.
The getWindowHandle method is used to store the window handle of the browser window in focus. We have to add import java.util.Set and import java.util.List statements to accommodate Set data structure in our code.
By default, the driver object can only access the elements of the parent window. In order to switch its focus from the parent to the child window, we shall take the help of the switchTo().window method and pass the window handle id of the child window as an argument to the method. Then to move from the child window to the parent window, we shall take the help of the switchTo().window method and pass the parent window handle id as an argument to the method.
Code Implementation.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import java.util.List; import java.util.Set; public class CloseSpecificWindow { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:Usersghs6korDesktopJavachromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("https://secure.indeed.com/account/login"); //implicit wait driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); //window handle of parent window String m = driver.getWindowHandle(); driver.findElement(By.id("login-google-button")).click(); // store window handles in Set Set w = driver.getWindowHandles(); // iterate window handles for (String h: w){ // switching to each window driver.switchTo().window(h); String s= driver.getTitle(); // checking specific window title if(s.equalsIgnoreCase("Sign in - Google Accounts")){ System.out.println("Window title to be closed: "+ driver.getTitle()); driver.close(); } } // switching parent window driver.switchTo().window(m); driver.quit(); } }
在配置自定义线程池时,如果没有调用`initialize()`方法,程序仍然可以正常运行的原因可能有以下几种: 1. **自动初始化**:某些线程池实现可能在首次使用时自动进行初始化。在这种情况下,即使你没有显式调用`initialize()`方法,线程池也会在需要时自动初始化。 2. **延迟初始化**:有些线程池设计支持延迟初始化,即在第一次提交任务时才进行初始化。如果你的代码在使用线程池之前没有显式调用`initialize()`方法,但随后提交了任务,那么线程池可能会在提交任务时自动初始化。
MySQL订单数据该如何高效划分:三个月内和三个月前?
Kubernetes Filebeat容器日志写入Elasticsearch失败?终极解决指南
Java函数式编程的未来发展
Spring Boot项目有多个启动类,如何指定打包后运行的启动类?
ThreadLocal存储请求上下文数据失效:为什么请求结束修改后数据未更新?