getWindowHandles()在firefox 58中不起作用。焦点仍然在父选项卡上,不会转移到下一个选项卡

时间:2018-03-07 00:23:34

标签: java selenium selenium-webdriver window-handles webdriverwait

我开始学习如何使用Selenium with Java在浏览器中处理多个选项卡。看起来我的代码不起作用。

stopwords.csv

请告诉我如何显示标题'数据政策'没有使用 getWindowHandles()。

2 个答案:

答案 0 :(得分:1)

getWindowHandles()工作得很好,但在调用getWindowHandles()之前,你必须按如下方式诱导 WebDriverwait

System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver= new FirefoxDriver();
driver.get("https://www.facebook.com/");
String parent= driver.getWindowHandle();
System.out.println("Parent Window is"+parent);
driver.findElement(By.linkText("Data Policy")).click();
WebDriverWait wait = new WebDriverWait(driver,5);
wait.until(ExpectedConditions.numberOfWindowsToBe(2));
Set<String> s1= driver.getWindowHandles();
for(String s2:s1)
{
    if(!parent.equalsIgnoreCase(s2))
        {
            driver.switchTo().window(s2);
            Thread.sleep(5000);
            System.out.println(driver.getWindowHandle());
            System.out.println("get title of window"+driver.getTitle());
        }
}

控制台输出:

Parent Window is4294967297
4294967303
get title of windowData Policy

答案 1 :(得分:0)

点击数据政策链接后,只需输入Thread.sleep(3000);

import java.util.ArrayList;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class HandlingWindows {

    public static WebDriver driver;
    public static void main(String[] args) throws InterruptedException {
        //D:\new folde backup\Selenium_project\seleniumproject\testNG_practice\driver
        System.setProperty("webdriver.gecko.driver", "./driver/geckodriver.exe");
        driver = new FirefoxDriver();
        driver.get("https://www.facebook.com/");
        String parent = driver.getWindowHandle();
        System.out.println("Parent Window is" + parent);

        // Get Data Policy
        WebElement we = driver.findElement(By.linkText("Data Policy"));
        // Click Data Policy link
        we.click();
        // Create an arrayList
        Thread.sleep(3000);
        ArrayList<String> s1 = new ArrayList<String>(driver.getWindowHandles());
        System.out.println(s1);
        for (String s2 : s1) {

            if (!(s2.equalsIgnoreCase(parent))) {
                driver.switchTo().window(s2);
                Thread.sleep(5000);
                System.out.println(driver.getWindowHandle());
                System.out.println("get title of window" + driver.getTitle());
            }
        }
    }

}

输出: -

Parent Window is4294967297
[4294967297, 4294967301]
4294967301
get title of windowData Policy