getwindowHandles()可以正常工作,但是在调用之前,getwindowHandles()您必须按照以下步骤诱使webdriverwait :
System.setProperty('webdriver.gecko.driver', 'C:UtilitybrowserDriversgeckodriver.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 is42949672974294967303get title of windowData Policy解决方法
我开始学习如何使用带Java的Selenium在浏览器中处理多个选项卡。看起来我下面的代码无法正常工作。
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 PolicyWebElement we= driver.findElement(By.linkText('Data Policy'));//Click Data Policy linkwe.click();//Create an arrayListArrayList<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()); } } }}
请让我知道如何在不使用getWindowHandles()的情况下显示标题“数据策略”。