我尝试在Chrome浏览器中使用Selenium(Java)按Ctrl+T

jm81lzqq  于 2022-12-13  发布在  Java
关注(0)|答案(1)|浏览(118)

我尝试在Chrome浏览器中使用Selenium(Java)按Ctrl+T。在控制台中没有错误,但浏览器没有打开新的标签页。我想知道为什么会发生这种情况。我尝试在Stack Overflow上查找,但我没有找到任何准确的答案。
这是我的代码:

package automation1;

import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class Action {

    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver",
                "C:\\Users\\asus\\OneDrive\\Desktop\\Jar\\chromedriver_win32\\chromedriver.exe");

        WebDriver chromedriver = new ChromeDriver();
        chromedriver.get("https://xenodochial-meninsky-118d47.netlify.app/");
        chromedriver.manage().window().fullscreen();
        
        Actions a = new Actions(chromedriver) ;
          Thread.sleep(3000) ;
        a.sendKeys(Keys.CONTROL +"t").build().perform() ;
        
    }

}

为什么这不会在Chrome浏览器中打开一个新标签?

tvokkenx

tvokkenx1#

有一个解决方案没有使用org.openqa.selenium.interactions.Actions

代码:

package tests;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class TejveerNaruka {

    public static void main(String[] args) {
        String chromedriverPath = System.getProperty("user.dir") + "\\resources\\chromedriver.exe";
        System.setProperty("webdriver.chrome.driver", chromedriverPath);
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--ignore-certificate-errors");
        options.addArguments("--start-maximized");
        options.addArguments("--disable-notifications");
        WebDriver driver = new ChromeDriver(options);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;

        driver.get("https://www.stackoverflow.com");
        System.out.println(driver.getTitle());
        
        // open new tab using javascript
        jsExecutor.executeScript("window.open()");
        
        // get Window handles, identifications of all browser tabs and windows
        List<String> windowHandles = new ArrayList<String> (driver.getWindowHandles());
        
        // switch to second tab
        driver.switchTo().window(windowHandles.get(1));
        
        // prints title of the second tab (empty line)
        System.out.println(driver.getTitle());

        // navigate driver in the second tab
        driver.get("https://www.zemancountdown.cz/");

        // prints title of the second tab           
        System.out.println(driver.getTitle());
        
        // close second tab
        jsExecutor.executeScript("window.close()");
        
        // switch to the first tab
        driver.switchTo().window(windowHandles.get(0));
        
        //prints title of the first tab 
        System.out.println(driver.getTitle());

        driver.quit();
    }

}

输出:

Starting ChromeDriver 108.0.5359.71 (1e0e3868ee06e91ad636a874420e3ca3ae3756ac-refs/branch-heads/5359@{#1016}) on port 18498
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Pro 12, 2022 12:50:43 ODP. org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Stack Overflow - Where Developers Learn, Share, & Build Careers

Miloš Zeman Countdown
Stack Overflow - Where Developers Learn, Share, & Build Careers

相关问题