简体   繁体   English

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

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

I have used the code below trying to open same multiple window "Google". 我使用下面的代码尝试打开相同的多个窗口“Google”。 Kindly help me in editing this and explaining how to handle this . 请帮助我编辑这个并解释如何处理这个问题。

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

and below code tried in Selenium: 以下代码在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();
    }
}

You can get a handle to your window via driver.getWindowHandle() and you can switch to a window with driver.switchTo().window("handle"); 您可以通过driver.getWindowHandle()获取窗口句柄,然后可以切换到带有driver.switchTo().window("handle"); .

If you want to open a new window you can click on a link with target="_blank" on the website or execute JavaScript to open a new window. 如果要打开新窗口,可以单击网站上target="_blank"的链接或执行JavaScript以打开新窗口。 Then you'll find another handle in driver.getWindowHandles() . 然后你会在driver.getWindowHandles()找到另一个句柄。 A possible way could be: 可能的方法是:

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

Another way is to inject the anchor and click it via JavaScript . 另一种方法是注入锚点并通过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);

Here is a sample example handling multiple windows: 以下是处理多个窗口的示例示例:

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.

相关问题 如何使用Java将单键(例如“ k”)发送到Selenium WebDriver中的窗口? - How to send single key (e.g. 'k') to window in Selenium WebDriver with Java? 如何使用Java处理Selenium Webdriver中的嵌套弹出窗口? - How to handle nested popup windows in selenium webdriver with java? 有没有办法为单个 Java 项目包含多个 Google API 服务(例如表格和文档)? - Is there some way to include multiple Google API services (e.g. Sheets and Docs) for a single Java project? Selenium Webdriver:是否可以检查资源(例如XML文件)是否已加载? - Selenium webdriver: can I check if a resource (e.g. XML file) has been loaded? 尝试使用带有Java的Selenium WebDriver处理多个浏览器窗口时获取java.util.NoSuchElementException - Getting java.util.NoSuchElementException while trying to handle multiple browser windows using selenium webdriver with java 如何使用带有Java的Selenium WebDriver多次单击同一按钮 - How to click on a same button multiple times using Selenium WebDriver with Java Java Path 如何转换为例如 InputStream - How is Java Path converted to e.g. InputStream 如何使用 java 在 Selenium WebDriver 中处理 iframe - How to handle iframe in Selenium WebDriver using java 如何使用Java处理Selenium Webdriver中的重叠页面 - How to handle overlapping pages in selenium webdriver with java 如何使用Java中的Selenium WebDriver处理身份验证弹出窗口 - How to handle authentication popup with Selenium WebDriver in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM