如何处理相同的多个窗口,例如使用 Java 在 Selenium WebDriver 中使用 google

问题描述 投票:0回答:4

我使用下面的代码尝试打开相同的多个窗口“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();
    }
}
java eclipse selenium-webdriver
4个回答
4
投票

您可以通过

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单击它


0
投票
//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);

0
投票

这是处理多个窗口的示例:

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);

  }

0
投票

我在处理多个窗口时面向。我正在尝试自动化三个窗口。就我而言,第二个和第三个窗口具有相同的标题名称。它正在移动到第三个窗口,但无法单击元素。这里我验证是否前往第三个窗口。我用了 (getCurrentUrl) 。但它正在打印第二个窗口 url。在自动化过程中,它正在进入第三个窗口。任何人都可以帮我解决这个问题吗?

© www.soinside.com 2019 - 2024. All rights reserved.