嗨,我正在学习Selenium &我不太清楚上面两个功能是如何工作的:问题陈述:
我有个练习作业说:去http://the-internet.herokuapp.com/
单击一个link>多窗口-- opens>,单击on>>,单击此处,从该窗口单击另一个窗口opens>>,抓取文本并在此之后打印,返回到该http://the-internet.herokuapp.com/windows并打印文本。
流程:http://the-internet.herokuapp.com/>>>http://the-internet.herokuapp.com/windows>>>http://the-internet.herokuapp.com/windows/new
( Que1)如果我使用driver.getWindowHandle()并为每个窗口打印它,它的值保持不变,所以这个方法总是返回父窗口,或者它的工作方式不同。
Ques2)当我使用driver.getWindowHandles()时,它将返回集合中的两个值。driver.getWindowHandles()也返回父窗口吗?(我不确定是否应该有2或3个值,因为我有3个URLS,我认为集合应该有3个)
( Ques3)是否可以共享使用多个子窗口id的最有效方法:
使用迭代器方法设置,人们也将Set转换为Arraylist,然后使用get方法。这是一个更好的方法
代码:
driver.get("http://the-internet.herokuapp.com/");
String p1=driver.getWindowHandle();
System.out.println(p1);
text1=driver.findElement(By.xpath("//a[@href='/windows']"));
text1.click();
WebElement
text2=driver.findElement(By.xpath("//a[@href='/windows/new']"));
text2.click();
Set<String> child=driver.getWindowHandles();
System.out.println(child.size());
ArrayList<String> children=new ArrayList<String>(child);
System.out.println(children);
driver.switchTo().window(children.get(1));
System.out.println(driver.findElement(By.xpath("//div[@class='example']/h3")).getText());
driver.close();
driver.switchTo().window(children.get(0));
System.out.println(driver.findElement(By.xpath("//div[@class='example']/h3")).getText());
driver.switchTo().window("");
System.out.println(driver.getCurrentUrl());
driver.close();
发布于 2021-05-14 13:47:24
driver.get("http://the-internet.herokuapp.com/");
String p1=driver.getWindowHandle(); //Gets the newly opened and the only window handle
System.out.println("This is parent window handle " + p1);
text1=driver.findElement(By.xpath("//a[@href='/windows']"));
text1.click(); //Navigates, no new window opened. Handle remains the same
//WebElement 'Unnecessary code
text2=driver.findElement(By.xpath("//a[@href='/windows/new']"));
text2.click(); //opens second window/tab as per the settings. there are 2 window handles here for current driver instance
Set<String> child=driver.getWindowHandles(); //set of 2
System.out.println(child.size());
// ArrayList<String> children=new ArrayList<String>(child);' modifying this
String strSecondWindowHandle = "";
for(String str : s) // as set is not an ordered collection we need to loop through it to know which position holds which handle.
{
if(str.equalsIgnoreCase(p1) == false) //to check if the window handle is not equal to parent window handle
{
driver.switchTo().window(str) // this is how you switch to second window
strSecondWindowHandle = str;
break;
}
}
// System.out.println(children);
// driver.switchTo().window(children.get(1)); //not required
System.out.println(driver.findElement(By.xpath("//div[@class='example']/h3")).getText());
driver.close(); // now this will close the second window
driver.switchTo().window(p1); // switches to main window
System.out.println(driver.findElement(By.xpath("//div[@class='example']/h3")).getText());
// driver.switchTo().window(""); //not required as it is the same window
System.out.println(driver.getCurrentUrl());
driver.close(); //closes the main window
所以回答你的问题
窗口句柄由操作系统(Windows)自动和唯一地分配给每个新打开的窗口
Q1 -直到并且除非您明确地切换窗口,否则窗口句柄将保持不变。切换是这里的关键。
Q2 -->导航不更改句柄。它不是特定于页面,而是特定于窗口。getWindowHandles将返回当前正在运行的WebDriver实例打开的所有打开的浏览器窗口。已经打开的窗口不包括在内。
Q3 ->使用上面演示的for循环,打开窗口,找到不是父窗口句柄的ID,将其存储在变量中。对于更多的窗口,请重复此过程。
发布于 2021-05-12 00:59:29
您可以在文档中看到getWindowHandle()
和getWindowHandles()
方法之间的主要区别:
getWindowHandle()
:向此窗口返回一个不透明句柄,该句柄在此驱动程序实例中唯一标识它。
getWindowHandles()
:返回一组窗口句柄,这些句柄可以通过将窗口传递给switchTo().WebDriver.Options.window()
来迭代此WebDriver实例的所有打开窗口。
简单地说,driver.getWindowHandles()
存储同时打开的所有页面的句柄集,但是driver.getWindowHandle()
获取焦点页面的句柄。它获取活动浏览器的地址,并具有返回类型的字符串。
https://stackoverflow.com/questions/67495846
复制相似问题