You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Selenium 3.9.1在Chrome v66.0.3开新标签?已有方案失效求替代

针对Selenium 3.9.1 + Chrome 66.0.3的新标签页替代方案

我懂你现在的困扰——用常规快捷键方法开新标签页完全不管用,结合你的版本组合,咱们试试几个更靠谱的路子:

1. 优先尝试JavaScript执行打开新标签页

这个方法绕开了快捷键可能被浏览器设置或页面元素拦截的问题,在你的版本里兼容性更稳:

// 调用JS直接打开空白新标签页
((JavascriptExecutor) driver).executeScript("window.open('','_blank');");
// 获取所有窗口句柄并切换到新标签页
ArrayList<String> tabs = new ArrayList<>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
// 访问目标网站
driver.get("http://google.com");

2. 先排查之前代码的小错误

你之前的解决方案1里,快捷键写的是Keys.CONTROL + "/t",这里多了个斜杠!正确的应该是Keys.CONTROL + "t",改完之后可以再试试原方法,说不定只是拼写失误导致的失效:

driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
ArrayList<String> tabs = new ArrayList<>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
driver.get("http://google.com");

3. 禁用浏览器弹窗拦截

如果上面的方法还是不行,可能是Chrome默认拦截了新标签页弹出,咱们启动浏览器时加个参数关闭拦截:

ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-popup-blocking");
WebDriver driver = new ChromeDriver(options);

// 之后再用JS或快捷键方法打开新标签页
((JavascriptExecutor) driver).executeScript("window.open('http://google.com','_blank');");
ArrayList<String> tabs = new ArrayList<>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));

内容的提问来源于stack exchange,提问作者arunkumar sambu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:58:00