Example on Switch to Parent Frame
Note: You need to create a maven project and you need add Selenium 4.0.0-alpha-3 dependency.
Scenario
print the innerHTML/text of the parent frame.
Print the innerHTML/text of the child frame.
Then again print innerHTML/Text of the Parent
frame.
import
org.openqa.selenium.By;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.WebElement;
import
org.openqa.selenium.chrome.ChromeDriver;
import
io.github.bonigarcia.wdm.WebDriverManager;
public class parentFrameexp
{
public static void main(String[] args) {
WebDriverManager.chromedriver().setup();
WebDriver
driver=new ChromeDriver();
driver.get("https://www.quackit.com/html/tags/html_iframe_tag.cfm");
String
pageTitle=driver.getTitle();
System.out.println("Page
Title "+pageTitle);
WebElement
parentFrame=driver.findElement(By.xpath("//iframe[@name='result4']"));
driver.switchTo().frame(parentFrame);
String
parentTitle=driver.findElement(By.tagName("title")).getAttribute("innerHTML");
System.out.println("Parent Frame
innerText is "+parentTitle);
WebElement
childFrame=driver.findElement(By.xpath("//iframe[@src='/html/tags/html_iframe_tag_example.cfm']"));
driver.switchTo().frame(childFrame);
String
childText=driver.findElement(By.tagName("a")).getText();
System.out.println("child Frame
innerText "+childText);
driver.switchTo().parentFrame();
parentTitle=driver.findElement(By.tagName("title")).getAttribute("innerHTML");
System.out.println("Parent Frame
innerText is "+parentTitle);
}
}
In Selenium 3 If you
want to access parent frame first you have to switch to default frame means
main webpage then you have to switch to parent , but in selenium 4 directly you
can switch over to parent frame without going to default frame.
Comments
Post a Comment