-2

I have frame where id and name is same and I want to switch the frame. How can I do this?

This what I tried:

driver.switchTo().frame("iframe");
driver.switchTo().frame("clntcap_frame");
driver.switchTo().frame("//iframe[@id='clntcap_frame']");

HTML code

<iframe name="clntcap_frame" id="clntcap_frame" style="" xpath="1"></iframe>

In console:

Exception in thread "main" org.openqa.selenium.NoSuchFrameException: No frame element found by name or id iframe

halfer
  • 19,824
  • 17
  • 99
  • 186
Preeti
  • 7
  • 7

1 Answers1

0

First, make sure that the iframe is present in the DOM. If it is present then followings should work l.

Try this: In this method, you try to pass the frame element itself.

    //To check the element exists or not but avoiding any exception, find all the elements with the id and put them in a list. Since id is a unique identifier, it should return only one.
    List<WebElement> frame = driver.findElements(By.id("clntcap_frame"));


   //Then check if the list of elements is empty or not. If not, then the frame is present in the DOM
    if(!frame.isEmpty()) {

        //Since frame is a list we need to get the element in the index 0
        driver.switchTo().frame(frame.get(0));
    } else {
            System.out.println("Frame with this id doesn't exists");
        }
S Ahmed
  • 1,454
  • 1
  • 8
  • 14
  • This what I got : Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//iframe[@id='clntcap_frame']"} – Preeti Apr 22 '19 at 18:21
  • Ho do I check if It is in DOM or not? I don't know this please reply – Preeti Apr 22 '19 at 18:24
  • @Preeti updated the code. check now. – S Ahmed Apr 22 '19 at 18:37
  • 1
    This has worked for me : new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.name("frame_name"))); – Preeti Apr 23 '19 at 12:55