python - Python Selenium 关闭选项卡
问题描述
我Selenium
在我的Python
项目中使用,有时会打开一个新选项卡(我想使用此选项卡并关闭第一个选项卡),我使用以下代码进行操作:
window_before = driver.window_handles[0]
driver.switch_to_window(window_before)
driver.close()
time.sleep(2)
之后,我尝试加载一个新的 URL:
driver.get(mainUrl)
我得到这个错误:
Exception has occurred: NoSuchWindowException
Message: no such window: target window already closed
from unknown error: web view not found
知道有什么问题吗?
解决方案
您需要返回未关闭的 TAB,例如:
window_to_close = driver.window_handles[0]
window_to_keep = driver.window_handles[1]
driver.switch_to_window(window_to_close)
driver.close()
driver.switch_to_window(window_to_keep)
driver.get(mainUrl)
推荐阅读
- ios - 请问,如果iOS设备上的屏幕打开或关闭,我如何使用FireMonkey?
- python - 射线 + cross_val_score
- ruby-on-rails - 在 rails 中编辑表单 - 没有路线匹配 [PUT]
- javascript - 水平导航栏下拉菜单中出现双边框
- node.js - 如果不发送图像,我怎么能在我的用户上实现默认图像?
- flutter - flutter_local_notifications 不适用于 ios,但适用于 android
- mariadb - 我缺少什么语法?
- python - Bash (Cygwin) 无法识别 Python 模块
- symfony - 不支持内容类型 \"text/plain\"
- google-cloud-platform - GCP 中自定义服务帐号的自定义 IAM 策略绑定