如何在Selenium WebDriver中切换回父窗口?

时间:2016-08-24 08:33:19

标签: java selenium-webdriver

我需要关闭子窗口并切换回父窗口以执行某些操作。

public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://www.example.com/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

@Test
public void testUntitled2() throws Exception {
    driver.get(baseUrl + "/info.php");
    driver.findElement(By.cssSelector("i.fa.fa-facebook-sq")).click();
    for (String winHandle : driver.getWindowHandles()) {
        driver.switchTo().window(winHandle);
    }

    // Perform the actions on new window
    driver.close(); // This will close new opened window

    //driver.switchTo().window(winHandleBefore); // I need to perform below

    driver.findElement(By.cssSelector("i.fa.fa-twitter-square")).click();
    driver.findElement(By.cssSelector("i.fa.fa-google-plus-  squ)).click();
    driver.findElement(By.cssSelector("i.fa.fa-linkedin-square")).click();
}

2 个答案:

答案 0 :(得分:0)

您可以使用此代码切换到子窗口,然后返回父窗口。

代码:

df.createOrReplaceTempView("table")
spark.sql("select url, count(url) from table where url like 'job'").show()

我希望这能解决你的问题

答案 1 :(得分:0)

parentWindowHandle循环之前创建一个for字符串变量并在那里存储窗口句柄。使用子窗口完成操作后,请返回parentWindowHandle

String parentWindowHandle = driver.getWindowHandle();
/* You code to move to child window*/

//After you done with child window
driver.switchTo().window(parentWindowHandle);