2013-07-13 91 views
4

我已經使用下面的代碼試圖打開相同的多個窗口「谷歌」。請幫助我編輯並解釋如何處理此問題。如何處理相同的多個窗口,例如谷歌在Selenium WebDriver與Java

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

和下面的代碼試圖在硒:

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(); 
    } 
} 
+0

其實你想幹什麼?你想打開一個新的標籤或新窗口具有相同的URL(你的情況是https://www.google.com/? –

回答

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

另一種方法是inject the anchor and click it via 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); 

    } 
相關問題