简体   繁体   English

我可以使用 Selenium 和 python 关闭手动打开的选项卡吗?

[英]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.

相关问题 使用 Python Selenium 如何关闭单击网页上的合法链接时打开的当前选项卡 - Using Python Selenium how to close the current tab which got opened when clicked a legal link on webpage Python Selenium 关闭选项卡 - Python Selenium close tab 如何关闭一个选项卡,然后使用 selenium 转到另一个选项卡? - How can I close a tab then go to the other tab with selenium? 如何在 Python 中使用 Selenium 完全关闭选项卡? - How do you fully close a tab by using Selenium in Python? Python Selenium - 无法使用谷歌快捷方式关闭标签 - Python Selenium - Cannot close tab using google shortcuts 如何使用Selenium,Chrome驱动程序和Python关闭新建的标签页 - How to close newly constructed tab using selenium, chrome driver and python 如何使用 selenium web 驱动程序 python 访问当前选项卡中新打开的小选项卡 - How to access newly opened small tab in the current tab using selenium web driver python 我可以在Python中关闭用subprocess.Popen打开的CMD窗口吗? - Can I close the CMD window that opened with subprocess.Popen in Python? 如何强制使用python打开套接字关闭? - How can I force socket opened with python to close? 如何使用 selenium 和 chromedriver 和 python 获取页面源代码而不显示打开的页面? - How can I get the page source, without showing the page opened, using selenium with chromedriver and python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM