簡體   English   中英

如何處理相同的多個窗口,例如Selenium WebDriver with Java中的google

[英]How to handle same multiple windows e.g. google in Selenium WebDriver with Java

我使用下面的代碼嘗試打開相同的多個窗口“Google”。 請幫助我編輯這個並解釋如何處理這個問題。

driver.switchTo().window("gbar");//not sure how to use this

以下代碼在Selenium中嘗試過:

package Testing;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import junit.framework.*;

public class Float {

    public static void setUp() {

        WebDriver driver = new FirefoxDriver();
        driver.navigate().to("https://www.google.com");
        driver.manage().window().maximize();
    }

    public static void main(String[] args) throws InterruptedException {

        WebDriver driver = new FirefoxDriver();
        driver.navigate().to("https://www.google.com");
        driver.manage().window().maximize();
        WebElement element = driver.findElement(By.name("q"));
        element.click();
        WebDriverWait wait = new WebDriverWait(driver, 80);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("q")));
        element.sendKeys("hi");
        element.clear();
        Thread.sleep(2000);
        element.sendKeys("hey");
        element.submit();
        setUp();

        driver.switchTo().window("gbar");// //* not sure how to use this *///
        WebElement element1 = driver.findElement(By.name("q"));
        element1.click();
        element1.sendKeys("hi");
        element1.clear();
        element1.sendKeys("hey");
        element1.submit();
        driver.quit();
    }
}

您可以通過driver.getWindowHandle()獲取窗口句柄,然后可以切換到帶有driver.switchTo().window("handle");

如果要打開新窗口,可以單擊網站上target="_blank"的鏈接或執行JavaScript以打開新窗口。 然后你會在driver.getWindowHandles()找到另一個句柄。 可能的方法是:

WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com");
List<String> knownHandles = new ArrayList<String>();
knownHandles.add(driver.getWindowHandle());
((JavascriptExecutor)driver).executeScript("window.open();");
// find the new handle. we are getting a set 
for (String handle : driver.getWindowHandles()) {
    if (!knownHandles.contains(handle)) {
        knownHandles.add(handle);
        break;
    }
}
String newHandle = knownHandles.get(knownHandles.size() -1 );
driver.switchTo().window(newHandle);
driver.get("https://www.google.com");

另一種方法是注入錨點並通過JavaScript單擊它

//Store the current window handle

String winHandleBefore = driver.getWindowHandle();

//Switch to new window opened

for(String winHandle : driver.getWindowHandles()){
    driver.switchTo().window(winHandle);
}

// Perform the actions on new window

driver.manage().window().maximize();

//Close the new window, if that window no more required

driver.close();

//Switch back to original browser (first window)

driver.switchTo().window(winHandleBefore);

以下是處理多個窗口的示例示例:

public class Mytesting {
 WebDriver driver = new FirefoxDriver();

 @Before
 public void beforetest() {
  driver.manage().window().maximize();
  driver.get("http://only-testing-blog.blogspot.in/2014/01/textbox.html");
 }

@Test
  public void test () throws InterruptedException 
  {
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
  driver.findElement(By.xpath("//b[contains(.,'Open New Page')]")).click();

  // Get and store both window handles in array
  Set<String> AllWindowHandles = driver.getWindowHandles();
  String window1 = (String) AllWindowHandles.toArray()[0];
  System.out.print("window1 handle code = "+AllWindowHandles.toArray()[0]);
  String window2 = (String) AllWindowHandles.toArray()[1];
  System.out.print("\nwindow2 handle code = "+AllWindowHandles.toArray()[1]);

  //Switch to window2(child window) and performing actions on it.
  driver.switchTo().window(window2);
  driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("My Name");
  driver.findElement(By.xpath("//input[@value='Bike']")).click();
  driver.findElement(By.xpath("//input[@value='Car']")).click();
  driver.findElement(By.xpath("//input[@value='Boat']")).click();
  driver.findElement(By.xpath("//input[@value='male']")).click();
  Thread.sleep(5000);

  //Switch to window1(parent window) and performing actions on it.
  driver.switchTo().window(window1);
  driver.findElement(By.xpath("//option[@id='country6']")).click();
  driver.findElement(By.xpath("//input[@value='female']")).click();
  driver.findElement(By.xpath("//input[@value='Show Me Alert']")).click();
  driver.switchTo().alert().accept();
  Thread.sleep(5000);

  //Once Again switch to window2(child window) and performing actions on it.
  driver.switchTo().window(window2);
  driver.findElement(By.xpath("//input[@name='fname']")).clear();
  driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("Name Changed");
  Thread.sleep(5000);
  driver.close();


  //Once Again switch to window1(parent window) and performing actions on it.
  driver.switchTo().window(window1);
  driver.findElement(By.xpath("//input[@value='male']")).click();
  Thread.sleep(5000);

  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM