python打开谷歌浏览器新标签页_python webdriver在Chrome浏览器中添加硒新标签页

博主遇到无法用Python在Chrome中打开新标签页的问题,其需求是打开新标签页操作后关闭并返回原标签页。原在Firefox中可行的代码在Chrome中无效,给出解决方案,用driver.execute_script(\window.open('','_blank');\)可打开空白新标签页,指定URL则用driver.execute_script(\window.open('URL');\)。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I am not being able to open a new tab in chrome. My requirement is to open a new tab do some operation then close this new tab and come back to old tab.

The below python code worked in Firefox but not working in Chrome. Could anyone please help me?

ActionChains(driver).key_down(Keys.CONTROL,body).send_keys('t').key_up(Keys.CONTROL).perform()

解决方案

Guess this will be helpful:

from selenium import webdriver

driver = webdriver.Chrome()

driver.execute_script("window.open('','_blank');")

This piece of code should start new Chrome browser session and open blank page in new tab

Use driver.execute_script("window.open('URL');") to open new tab with required URL

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值