How to Switch Between Frames in Selenium Webdriver Using Java

How to switch between frames in Selenium WebDriver using Java

WebDriver's driver.switchTo().frame() method takes one of the three possible arguments:

  • A number.

    Select a frame by its (zero-based) index. That is, if a page has three
    frames, the first frame would be at index 0, the second at index 1
    and the third at index 2. Once the frame has been selected, all
    subsequent calls on the WebDriver interface are made to that frame.

  • A name or ID.

    Select a frame by its name or ID. Frames located by matching name
    attributes are always given precedence over those matched by ID.

  • A previously found WebElement.

    Select a frame using its previously located WebElement.

Get the frame by it's id/name or locate it by driver.findElement() and you'll be good.

How to switch between multiple frames using Selenium and Java

When you need to switch between two child frames of the same parent frame (e.g. Top Level Frame) you need to switch to the defaultContent which is either the first frame on the page, or the main document when a page contains iframes and then switch to the second child frame as follows:

  • First code block:

    cpp.fillintextfields.get(4).sendKeys("test@test.com");
    WebElement es = driver1.findElement(By.id("CollectJSInlineccexp"));
    driver1.switchTo().frame(es);
    cpp.expdate.sendKeys("01/21");
    driver1.switchTo().defaultContent();
    Thread.sleep(2000);
    driver1.switchTo().frame("CollectJSInlineccnumber");
    Thread.sleep(2000);
    cpp.cdnumber.sendKeys("4111111111111111");
  • Second code block:

    cpp.fillintextfields.get(4).sendKeys("test@test.com");
    driver1.switchTo().frame("CollectJSInlineccnumber");
    Thread.sleep(2000);
    cpp.cdnumber.sendKeys("4111111111111111");
    Thread.sleep(5000);
    driver1.switchTo().defaultContent();
    WebElement es = driver1.findElement(By.id("CollectJSInlineccexp"));
    driver1.switchTo().frame(es);
    cpp.expdate.sendKeys("01/21");

References

You can find a couple of relevant discussions in:

  • Multiple iframe tags Selenium webdriver

How to expedite switching between iFrames in Selenium Webdriver with Java code?

You can store a frame as an element variable first and then switch to it.

Here is an example:

var frameExample = driver.FindElement(By.className("myFrame"));
driver.switchTo().frame(frameExample);

So essentially, you store frames in element variables and switch when needed, it will help with performance issues.

How to switch to and from in Selenium

To switch to frame :

driver.switchTo().frame(0);

To switch to main window back :

driver.switchTo().defaultContent();

Also I suggest to find element of frame first do switch and then find element.

You can switch to frame by following 3 ways :

By Frame Index

By Frame ID or Name

By Frame WebElement

For More Details Please check : Switch to Frame in Selenium

I hope it help you..

Switching to iframes in Selenium-Java

Try to execute this code.

WebElement iframe = driver.findElement(By.tagName("iframe"));
driver.switchTo().frame(iframe); //Move inside to the frame.
WebElement body = driver.findElement(By.tagName("body"));
body.click();
driver.findElement(By.xpath("//a[text()='Enter Country']")).click();
Select dropdown = new Select(driver.findElement(By.xpath("//select[@id='combobox']")));
dropdown.selectByVisibleText("Portugal");
driver.switchTo().defaultContent(); //Move outside to the frame.

WebDriver switch between sibling frames

From what I remember, there is no such thing called "switch between sibling frames". What you were doing should be correct in theory (except for if you want to go inside main from default content, you need to switch to smgleft first)

So have you tried

driver.switchTo().defaultContent();
driver.switchTo().frame("smgleft");
// maybe some debugging here see if you can find frame "main" now
driver.switchTo().frame("main");


Related Topics



Leave a reply



Submit