java selenium selenium-webdriver window-handles webdriverwait

java - getWindowHandles() no funciona en firefox 58.El foco permanece en la pestaña principal y no se transfiere a la siguiente pestaña



selenium selenium-webdriver (2)

Estoy empezando a aprender cómo manejar múltiples pestañas en un navegador usando Selenium con Java. Parece que mi código a continuación no funciona.

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 void main(String[] args) throws InterruptedException { WebDriver 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 ArrayList<String> s1= new ArrayList<String>(driver.getWindowHandles()); 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()); } } } }

Avíseme cómo puedo mostrar el título ''Política de datos'' sin usar getWindowHandles ().


Solo pon Thread.sleep(3000); después de hacer clic en el enlace Política de datos.

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

Salida:-

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


getWindowHandles() funciona bastante bien, pero antes de invocar getWindowHandles() debe inducir WebDriverwait de la siguiente manera:

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

Salida de consola:

Parent Window is4294967297 4294967303 get title of windowData Policy