java Selenium:如何知道当前的帧数或值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25177674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium : How to know current frame number or value?
提问by Puneet Purohit
I am working on a page which have some pop ups. I want to test all the HTML element availability on all the pop ups. SO to know current frame no. Is there any method available like Selenium.getFrameNumber()
or something so that I can switch to required frames using
我正在处理一个有一些弹出窗口的页面。我想在所有弹出窗口上测试所有 HTML 元素的可用性。所以要知道当前帧号。是否有任何可用的方法Selenium.getFrameNumber()
或其他方法,以便我可以使用以下方法切换到所需的帧
Selenium.switchTo().frame(0); or Selenium.switchTo().defaultContent();
回答by Vignesh Paramasivam
There is no default option available for that, but you can get this in different ways,
没有可用的默认选项,但您可以通过不同的方式获得它,
Store the list of iframes in the page,
将 iframe 列表存储在页面中,
List<WebElement> myIframes= driver.findElements(By.tagName("iframe"));
and you can iterate to get to the required frame with their id.
并且您可以迭代以使用它们的 id 到达所需的帧。
you can get the total no. of frames,
你可以得到总数。帧数,
myIframes.size();
回答by djangofan
In one project I made, I used a ArrayList to keep track of the windows.
在我制作的一个项目中,我使用了一个 ArrayList 来跟踪窗口。
List<String> windowList = new ArrayList<String>();
windowList.add( driver.getWindowHandle() );
The initial window always populates the first entry in the list and never changes.
初始窗口总是填充列表中的第一个条目并且永远不会改变。
Subsequent windows get added onto the END of the list like so:
随后的窗口被添加到列表的 END 上,如下所示:
driver.clickButtonToOpenPopup();
windowList.add( driver.getWindowHandle() );
Then, when a popup window (or windows) or tab is closed, I decrement the list in the order than I opened the windows.
然后,当一个弹出窗口(或多个窗口)或选项卡关闭时,我按照打开窗口的顺序减少列表。
pos = windowList.size();
if ( pos > 1 && driver.closeCurrentWindow() && windowList.remove() ) {
log.info( "Closed window at position #" + pos );
} else {
log.info("There was a problem closing the window.");
}
Lots of different ways you could do this.
有很多不同的方法可以做到这一点。
If I were to do it again, I might use the ArrayDeque class.
如果我再做一次,我可能会使用ArrayDeque 类。