Selenium Web Driver Handling Frames
It is impossible to click iframe directly through XPath since it is an iframe. First we have to switch to the frame and then we can click using xpath.
driver.switchTo().frame()
has multiple overloads.
driver.switchTo().frame(name_or_id)
Here your iframe
doesn't have id or name, so not for you.
driver.switchTo().frame(index)
This is the last option to choose, because using index is not stable enough as you could imagine. If this is your only iframe in the page, try driver.switchTo().frame(0)
driver.switchTo().frame(iframe_element)
The most common one. You locate your iframe like other elements, then pass it into the method.
driver.switchTo().
defaultContent(); [parentFrame, defaultContent, frame]
// Based on index position:
int frameIndex = 0;
List<WebElement> listFrames = driver.findElements(By.tagName("iframe"));
System.out.println("list frames "+listFrames.size());
driver.switchTo().frame(listFrames.get( frameIndex ));
// XPath|CssPath Element:
WebElement frameCSSPath = driver.findElement(By.cssSelector("iframe[title='Fill Quote']"));
WebElement frameXPath = driver.findElement(By.xpath(".//iframe[1]"));
WebElement frameTag = driver.findElement(By.tagName("iframe"));
driver.switchTo().frame( frameCSSPath ); // frameXPath, frameTag
driver.switchTo().frame("relative=up"); // focus to parent frame.
driver.switchTo().defaultContent(); // move to the most parent or main frame
// For alert's
Alert alert = driver.switchTo().alert(); // Switch to alert pop-up
alert.accept();
alert.dismiss();
XML Test:
<html>
<IFame id='1'>... parentFrame() « context remains unchanged. <IFame1>
|
-> <IFrame id='2'>... parentFrame() « Change focus to the parent context. <IFame1>
</html>
</html>
<frameset cols="50%,50%">
<Fame id='11'>... defaultContent() « driver focus to top window/first frame. <html>
|
-> <Frame id='22'>... defaultContent() « driver focus to top window/first frame. <Fame11>
frame("relative=up") « focus to parent frame. <Fame11>
</frameset>
</html>
Conversion of RC to Web-Driver Java commands. link.
<frame>
is an HTML element which defines a particular area in which another HTML document can be displayed. A frame should be used within a <frameset>
. « Deprecated. Not for use in new websites.