[英]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.我试图关闭具有未使用 webdriver 打开的特定 URL 的选项卡。 So when I search all solutions contained this line:因此,当我搜索包含此行的所有解决方案时:
driver = webdriver.Chrome()
which actually opens a new window and control that.它实际上打开了一个新的 window 并控制它。 I also tried我也试过
print(driver.window_handles)
that actually does not print any window handles that were opened manually.实际上不会打印任何手动打开的 window 句柄。 My requirement is something like I open a lot of geeksforgeeks.com(let's say) tabs while working.我的要求就像我在工作时打开很多 geeksforgeeks.com(比方说)标签。 I want a script that will close all geeksforgeeks tabs in my browser.我想要一个脚本来关闭我浏览器中的所有 geeksforgeeks 选项卡。 Is that possible?那可能吗?
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.请根据您的要求更改上述代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.