无法使用Selenium Webdriver在浏览器中打开新选项卡 [英] Not able to open new tab in Browser using Selenium Webdriver

查看:188
本文介绍了无法使用Selenium Webdriver在浏览器中打开新选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用下面的代码行在硒中打开新标签页

I am trying to open new tab in selenium using below line of code

driver.findElement(By.cssSelector("body")).sendKeys(Keys.chord(Keys.CONTROL, "t"));

但是选项卡没有打开.谁能说出此命令的错误之处? 还可以解释此命令中使用的"driver.findElement(By.cssSelector("body"))"吗?我尝试搜索但未找到正确的答案

But tabs is not opening up. Can anyone tell whats wrong in this command? Also can any one explain "driver.findElement(By.cssSelector("body"))" used in this command for ? I tried searching but not proper answers

完成以下操作无效.它在chrome的同一标签中同时打开gmail和堆栈溢出,但未打开新标签

Below complete is not working. It is opening up both gmail and stack overflow in same tab in chrome not opening up new tab

package TestCode;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Chrome {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver","C:\\Akash\\Drivers\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.gmail.com");
        String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL+"t"); 
        driver.findElement(By.tagName("body")).sendKeys(selectLinkOpeninNewTab);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://stackoverflow.com/");
        System.out.println("Site open");
    }

}

推荐答案

您可以使用javaScripts在chrome中打开新标签页.

You can use javaScripts to open new tab in chrome.

在代码行下面尝试

  JavascriptExecutor js = (JavascriptExecutor) driver; 
 js.executeScript("window.open('https://www.google.com','_blank');");

请参阅此链接:- 查看全文

相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆