I have web page which has two frames -navigator and content.I have to click on a link in the navigator and the content of the frame 'content' changes.Now I have to input some value in the content frame.I use IDE to record and generate code in the junit4 webdriver format.
selenium.selectFrame("navigator");
selenium.click("link=mylink");
selenium.selectFrame("relative=up");
selenium.selectFrame("content");
selenium.type("myinputfield", "7");
Now when I try to run this code I get error at relative=up.So I google and find that this is not the way to write webdriver code.I rewrote the entire code in the webdriver style coding .So now I have this code.
driver.switchTo().frame("navigator");
WebElement my_nav_link = driver.findElement(By.partialLinkText("MyLinkText"));
my_nav_link.click();
driver.switchTo().frame("content");
WebElement myinputfieldelem= driver.findElement(By.id("myinputfield"));
myinputfieldelem.sendKeys("7");
Now when I try again , it errors out at frame not found for content .
So I try to debug a bit and I add the below code just before switch to frame to see all the frames present.
List<WebElement> frameset=driver.findElements(By.tagName("frame"));
if(frameset.size()>0)
{
for (WebElement framename :frameset)
{
System.out.println("frame id:" + framename.getAttribute("name"));
}
}
There are no frames ,so that means I am now in navigator frame and I have to go one level up to find the frame content.
So I tried
driver.switchTo().frame("relative=up");
This also failed.Now I am clueless and and could not figure out what to do.After some googling i found out another option which saved me and the final code which works is as below.
driver.switchTo().frame("navigator");
WebElement my_nav_link = driver.findElement(By.partialLinkText("MyLinkText"));
my_nav_link.click();
driver.switchTo().defaultContent();
driver.switchTo().frame("content");
WebElement myinputfieldelem= driver.findElement(By.id("myinputfield"));
myinputfieldelem.sendKeys("7");
So the outcome is you have to use switchTo().defaultContent() to be able to come out of a frame and go to a sibling frame.