Selenium:通过索引关闭特定选项卡(句柄)而无需切换到它
是否可以在
目前,我正在这样做:
driver.switch_to.window(driver.window_handles[1])
driver.close()
driver.switch_to.window(driver.window_handles[0])
我发现切换选项卡会使我的脚本非常不稳定,因此我希望在可能的情况下避免使用。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
WebDriver可以关闭
窗口/TAB
实例,如果它专注于该特定TAB
或Windows
。因此,如果您专注于不同的
选项卡
或Windows
,则无法关闭选项卡或Windows
。如果您面对
不稳定
脚本执行时,请执行tabs/windows
我会说有time.sleep(2)
或time。睡眠(3)
应该可以帮助您解决或摆脱不稳定。还请记住,
驱动程序
将关闭所有实例。WebDriver can close a
window/tab
instance if it has the focus on that particulartab
orwindows
.So you can not close a
tab or windows
if you are focusing on a differenttab
orwindows
.If you are facing
unstable
script execution while switchingtabs/windows
I'd say havetime.sleep(2)
ortime.sleep(3)
that should help you resolve or get rid off un-stable.Also remember,
driver.quit
would close all the instances.实际上,如果使用 chromedriver ,但它需要使用 Chrome开发人员工具协议(在硒4中支持)。
更具体地说:您必须:
targetID
。Actually it is possible if using chromedriver but it requires to use Chrome Developer Tools Protocol (which is supported in Selenium 4).
More specifically you have to:
targetId
of the page (target) you want to close.targetId
.