Chrome中的Selenium Java新標簽 - 無法打開,在同一標簽中保持打開URL

[英]Selenium Java New Tab in Chrome - won't open , keeps opening URL`s in same tab


package javaapplication1;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class JavaApplication1 {
    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Aca\\desktop\\chromedriver.exe");
        // Initialize driver
        WebDriver driver = new ChromeDriver();
        //Maximize browser window
        driver.manage().window().maximize();
        //Go to URL
        driver.get("http://www.google.com");
        //Set timeout
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        // Open new tab  – May be you are stuck here
        driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
        //Go to URL
        driver.get("http://www.gmail.com");
        //Set new tab timeout
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);        
    }
}

I am trying to open a new tab, leaving the previous tab opened .. I can't get a new tab opened. It keeps opening URL`s in the same tab.. I also tried using Actions.

我正在嘗試打開一個新選項卡,打開上一個選項卡..我無法打開新選項卡。它在同一個標​​簽中保持打開URL。我也嘗試使用Actions。

4 个解决方案

#1


1  

You need to switch the driver to the new tab. In Chrome its done like switching windows

您需要將驅動程序切換到新選項卡。在Chrome中,它就像切換窗口一樣

String tabHandle = driver.getWindowHandle();

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

// switch to the new window
for (String handle : driver.getWindowHandles()) {
    if (!handle.equals(tabHandle))
    {
        driver.switchTo().window(handle);
    }
}

driver.get("http://www.gmail.com");

// close the new tab and switch back to the old one
driver.close();
driver.switchTo().window(tabHandle);

As a side note, implicitlyWait is defined for the driver, not tab/window. No need to define it again after opening the new tab.

作為旁注,隱含的是為驅動程序定義的,而不是選項卡/窗口。打開新選項卡后無需再次定義它。

#2


1  

This is an issue with chromedriver itself. See the related bug submitted

這是chromedriver本身的一個問題。查看提交的相關錯誤

#3


0  

You are forgetting to change the focus of the driver to the new tab before navigating.

在導航之前,您忘記將驅動程序的焦點更改為新選項卡。

Try this after opening a new tab:

打開新標簽后嘗試此操作:

driver.sendKeys(Keys.chord(Keys.CONTROL,"t"));
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles()); //Get tabs
driver.switchTo().window(tabs.get(1)); //Navigate to new tab
driver.get("http://www.gmail.com"); //Navigate within new tab
driver.close(); //Close the tab when done
driver.switchTo().window(tabs.get(0)); //Navigate to original tab

#4


0  

Please try this to open new tab in Chrome with Selenium Java.

請嘗試使用Selenium Java在Chrome中打開新標簽頁。

     package com.sample;



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

        public class NewWindow {

            public static void main (String[] args){
                System.setProperty ("webdriver.chrome.driver", "C:\\Chrome Driver\\chromedriver.exe");

                ChromeOptions options = new ChromeOptions(); 
                options.addArguments("disable-arguments");


                WebDriver driver = new ChromeDriver();
                driver.get("https://www.google.com");

((JavascriptExecutor) driver).executeScript("window.open('http://facebook.com/');");

        }
    }

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/12/04/d46000f969db4bf3e9c7f3ccee492a33.html



 
  © 2014-2022 ITdaan.com 联系我们: