python - 如何使用 selenium 打开和管理新标签页
问题描述
我正在尝试使用“https://www.gmail.com”网址打开一个新标签,然后选择一些信息并返回第一页,我正在使用 CTRL + t 命令打开新标签,但是,如何在两个页面之间切换?我的代码片段:
driver.find_elements_by_css_selector('body').send_keys(Keys.CONTROL + "t")
实际代码:
bot = self.bot
guids = bot.window_handles()
time.sleep(2)
bot.execute_script("window.open()")
bot.get('https://gmail.com')
time.sleep(1)
Python 版本 = Python 3.8.5
解决方案
#current window
first_tab = bot.window_handles[0]
#create new tab
bot.execute_script("window.open()")
#move to new tab
new_tab = bot.window_handles[1]
bot.switch_to.window(new_tab)
bot.get('https://gmail.com')
#switch to first tab
bot.switch_to.window(first_tab)
推荐阅读
- flutter - 尝试...抓住停止应用程序 (Dart/Flutter)
- html - 网络抓取:网络抓取的对象与网站上的信息不匹配,导致 RStudio 崩溃
- python - 大熊猫python中基于组的第n个十分位数的假人
- .net-core - 在 azure functions 3 中,我可以将日志过滤到不同级别的应用程序洞察和控制台
- java - 构建Andoid浏览器
- woocommerce - 如何在续订订单管理电子邮件中获取 WooCommerce 订阅父 ID?
- python - 熊猫 ffill 和 bfill 不能一起工作
- linux - 如何将 reaver 降级为 reaver 1.4 [Makefile:13:wash] 错误 1?
- php - INPUT 适用于 MAMP,但不适用于网络服务器
- python-3.x - 如何增加范围内的第一个数字?