'Can I close a manually opened tab using Selenium and python?

I was trying to close tabs having a specific URL that was not opened using webdriver. So when I search all solutions contained this line:

driver = webdriver.Chrome()

which actually opens a new window and control that. I also tried

print(driver.window_handles)

that actually does not print any window handles that were opened manually. My requirement is something like I open a lot of geeksforgeeks.com(let's say) tabs while working. I want a script that will close all geeksforgeeks tabs in my browser. Is that possible?



Solution 1:[1]

String parent=driver.getWindowHandle();

Set<String>s=driver.getWindowHandles();

Iterator<String> I1= s.iterator();
while(I1.hasNext())
{
String child_window=I1.next();

if(!parent.equals(child_window))
{
driver.switchTo().window(child_window);

window_title = driver.switchTo().window(child_window).getTitle();
if (window_title == "geeksforgeeks.com")
{
driver.close();
}

}
//switch to the parent window
driver.switchTo().window(parent);

Kindly change the above code as per your requirement.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Srinivas