4

I am unable to locate a frame inside an iframe. I have attached the screenshot of the html page.Html screenshot

I am able to locate iframe 'content' by xpath //div[@id='pageContentDiv']//iframe which I verified in chrome and my code is working.

WebElement frame1 = driver.findElement(By.xpath("//div[@id='pageContentDiv']//iframe"));
        driver.switchTo().frame(frame1);

but unable to locate the frame 'detailsDisplay'.I tried //div[@id='pageContentDiv']//iframe//frame[@name='detailsDisplay'] but unable to locate in chrome. My motive is to locate the 'Part Details' link element inside the 'detailsDisplay' frame. Full code.

public WebDriver createPart() {

try {

    driver.findElement(By.id("username")).sendKeys("502409373");
    driver.findElement(By.id("password")).sendKeys("Magic14Magic");
    driver.findElement(By.id("submitFrmShared")).click();
    //Thread.sleep(10000);
    driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
    driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);
    Select dropCountry = new Select(driver.findElement(By.id("txtNewLocation")));
    dropCountry.selectByVisibleText("India");
    driver.findElement(By.xpath("//button[@class='btn']/label")).click();
    driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
    driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);

    //driver.findElement(By.xpath("//span[@class='ds-coachmark-close']")).click();
    //driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
    //driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);     
    Thread.sleep(10000);
    String parentWindowHandler = driver.getWindowHandle(); // Store your parent window
    driver.findElement(By.xpath("//li[@class='icon-button add']/span")).click();
    driver.findElement(By.xpath("//div[@id='ENCActions']/a/label")).click();

    driver.findElement(By.xpath("//label[starts-with(text(),'Create Part...')]")).click();
    driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
    driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);       
    String subWindowHandler = null;     
    Set<String> handles = driver.getWindowHandles(); // get all window handles
    Iterator<String> iterator = handles.iterator();
    while (iterator.hasNext()){
        subWindowHandler = iterator.next();
        System.out.println("k1");
    }
    System.out.println(driver.switchTo().window(subWindowHandler).getTitle());

    WebDriverWait wait = new WebDriverWait(driver, 10);

    WebElement element = driver.findElement(By.xpath("//select[@id='Type-Field']//following-sibling::div//div//input"));

    element.click();
    Thread.sleep(3000);
    element.sendKeys(Keys.BACK_SPACE);
    Thread.sleep(3000);

    element.sendKeys("Subassy");
    Thread.sleep(4000);

    driver.findElement(By.xpath("//div[@data-value='Subassy']")).click();

    driver.findElement(By.xpath("//span[text()='Description']//parent::td//following-sibling::td//textarea")).sendKeys("Testing");
    driver.findElement(By.xpath("//option[text()='BioSc-DS-Chemical']//parent::select")).click();
    Thread.sleep(2000);
    driver.findElement(By.xpath("//option[text()='BioSc-DS-Chemical']")).click();
    driver.findElement(By.xpath("//a[text()='Done']")).click();
    driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
    driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);

    System.out.println(driver.switchTo().window(parentWindowHandler).getTitle());

    driver.switchTo().frame("content");

    driver.switchTo().frame("detailsDisplay");
    driver.findElement(By.xpath("//a[@title='Part Details']")).click();


} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
return driver;

}

I am adding 2 screenshots. In the first screenshot I am able to locate the iframe 'content'. In the second screenshot, I am unable to locate the 'detailsDisplay' frame (HTML code is not getting highlighted) by xpath //div[@id='pageContentDiv']//iframe//frame[@name='detailsDisplay']

screenshot 1 screenshot 2

Is there anything issue with the html tag and document tags in the code ? I am struggling with this issue quite a longtime. Please suggest some solutions.

3
  • Share the full code starting from how you switch iframe finishing with how you locate your link element.
    – Alexey R.
    May 29, 2018 at 9:51
  • I have added full code. May 29, 2018 at 10:12
  • @kiran kumar i am facing a similar issue. can you please share how u resolved this issue?
    – Ayisha
    Apr 12, 2019 at 7:07

3 Answers 3

3

If your code has nested frames, first switch to the parent frame. Then switch from the parent frame to the inner child frame where you want to locate the element.

Once you switched to the child frame, locate the element which you want.

//a[text()='Part Details'];
3
  • I tried facing unable to locate error. May 29, 2018 at 9:10
  • Have you tried with this xpath - //a[text()='Part Details']
    – shanila
    May 29, 2018 at 9:25
  • Unable to locate element: {"method":"xpath","selector":"//a[text()='Part Details']"} error occured May 29, 2018 at 10:25
1

1.Use the following code to switch into the frame:

driver.switchTo.frame(content); //now you have switched into the frame

2.Then you need to switch into the details display frame using the below code:

driver.switchTo.frame(detailsDisplay);

3.Then access the part details using the below xpath:

//a[contains(text(), 'Part Details')]
9
  • I tried these beforehand. But nothing is working out. I am able to switch to 'detailsDisplay' frame through code but facing Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//a[@title='Part Details']"} while trying to locate th element. In chrome I can locate content frame. but unable to locate 'detailsDisplay' frame. Inside detailsDisplay frame html code is included in #document. Maybe it has something to do with that. May 29, 2018 at 9:09
  • Your xpath seems to be attribute based and //a[contains(text(), 'Part Details')] seems to be a text based. please try this xpath and let me know still you are facing issues. May 29, 2018 at 9:29
  • Unable to locate element: {"method":"xpath","selector":"//a[text()='Part Details']"} error occured. May 29, 2018 at 10:25
  • Give browser.sleep or proper wait commands after switching into the frame and try to execute May 29, 2018 at 10:41
  • Tried that too. Its failing again. Firstly, I am unable to locate the detailsDisplay frame in chrome by xpath. I think if we can resolve that we can resolve this issue. May 29, 2018 at 10:51
1

I don't know if it works for python but that was how I solved this problem in Java:

the switchTo().frame() method returns a WebDriver that is focused on the targeted iframe. We can reach the sub Iframe using this webDriver like :

WebDriver subDriver = webDriver.switchTo().frame("iFrameName").switchTo().frame("subIFrameName");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.