getwindowhandle();
- >用于获取父窗口名称。
getwindowhandles():
- >用于获取所有子窗口链接。
但如何使用string&迭代器的概念?用于儿童窗户
答案 0 :(得分:0)
您可以使用以下代码 -
String mainwindow=driver.getWindowHandle(); //get parent(current) window name
for(String popup :driver.getWindowHandles()) //iterating on child windows
{
driver.switchTo().window(popup);
System.out.println("Popup values: "+popup);
// Perform any operations on child window
}
driver.switchTo().window(mainwindow); //switch back to main window & continue further execution
答案 1 :(得分:0)
试试这个。
public void SwitchToAnotherWindow(Webdriver driver,int window_number){
List<String> windowlist = null;
Set<String> windows = driver.getWindowHandles();
windowlist = new ArrayList<String>(windows);
String currentWindow = driver.getWindowHandle();
if (!currentWindow.equalsIgnoreCase(windowlist.get(window_number - 1)))
{
driver.switchTo().window(windowlist.get(window_number - 1));
}
}
只需传递窗口编号要执行的窗口,例如如果要切换到3窗口只需将3传递给此方法,它可以直接随机播放到任何窗口 (例如:1到9个窗口,或9到4个窗口)
答案 2 :(得分:0)
public static void main(String[] args) throws InterruptedException
{
WebDriver d = new FirefoxDriver();
d.get("url");
String prntwin = d.getWindowHandle();
System.out.println("Parent window's handle -> " + prntwin);
WebElement clkElement = d.findElement(By.id("button1"));
for(int i = 0; i < 3; i++)
{
clkElement.click();
Thread.sleep(3000);
}
Set<String> allWindowHandles = d.getWindowHandles();
for(String handle : allWindowHandles)
{
System.out.println("Window handle - > " + handle);
}
}
答案 3 :(得分:0)
public static void main(String[] args) throws InterruptedException {
ChromeOptions chromeOption = new ChromeOptions();
ChromeDriverManager.chromedriver().setup();
ChromeDriver driver = new ChromeDriver(chromeOption);
driver.get("https://the-internet.herokuapp.com/");
driver.manage().window().maximize();
driver.findElement(By.xpath("//a[contains(text(),'Multiple Windows')]")).click();
driver.findElement(By.xpath("//a[@target='_blank']")).click();
Set<String>id=driver.getWindowHandles();
Iterator<String> ids=id.iterator();
String Parent=ids.next();
String Child=ids.next();
driver.switchTo().window(Child);
System.out.println(driver.getTitle());
driver.switchTo().window(Parent);
System.out.println(driver.getTitle());
}
}