web-dev-qa-db-ja.com

Java

次のコードを使用して、ブラウザのタブを切り替える必要があります。

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

正常に機能していることもありますが、例外が発生していることもあります。誰かが私にJavaを使用して単一のウィンドウ内でタブを切り替えるための他の手順があることを示唆していますか?.

7
mahi

ここではウィンドウハンドル関数を使用する必要があります。 Javaのサンプル作業コードは次のとおりです。

    String parentHandle = driver.getWindowHandle(); // get the current window handle
    System.out.println(parentHandle);               //Prints the parent window handle 
    String anchorURL = anchor.getAttribute("href"); //Assuming u are clicking on a link which opens a new browser window
    anchor.click();                                 //Clicking on this window
    for (String winHandle : driver.getWindowHandles()) { //Gets the new window handle
        System.out.println(winHandle);
        driver.switchTo().window(winHandle);        // switch focus of WebDriver to the next found window handle (that's your newly opened window)              
    }
//Now your driver works on the current new handle
//Do some work here.....
//Time to go back to parent window
    driver.close();                                 // close newly opened window when done with it
    driver.switchTo().window(parentHandle);         // switch back to the original window

お役に立てれば!

8
Fahim Hossain

ブラウザウィンドウの切り替えは、b/wタブの切り替えとは異なります。

一部のブラウザでは、windowhandlerコマンドが機能する場合がありますが、すべてのブラウザで機能するわけではありません。

これは、b/wタブをナビゲートするためのソリューションです

左から右へ移動する場合:

Actions action= new Actions(driver);
action.keyDown(Keys.CONTROL).sendKeys(Keys.TAB).build().perform();

右から左に移動する場合:

Actions action= new Actions(driver);
action.keyDown(Keys.CONTROL).keyDown(Keys.SHIFT).sendKeys(Keys.TAB).build().perform();
3
nitesh

私の場合、次のコードは正常に機能しています

String oldTab=driver.getWindowHandle();
    driver.findElement(pageObj.getL_Popup_Window()).click();
     ArrayList<String> newTab = new ArrayList<String>(driver.getWindowHandles());
        newTab.remove(oldTab);
        driver.switchTo().window(newTab.get(0));
    WebElement ele = driver.findElement(pageObj.getI_input_name());
    ele.click();
    ele.sendKeys(name);
    driver.findElement(pageObj.getI_submit()).click();
    driver.switchTo().window(oldTab);
0