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.

kiran kumar
  • 49
  • 1
  • 1
  • 5

3 Answers3

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'];
Bharat Mane
  • 6,775
  • 10
  • 40
  • 67
shanila
  • 491
  • 2
  • 11
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')]
Bharat Mane
  • 6,775
  • 10
  • 40
  • 67
Prasanna venkatesh
  • 1,173
  • 1
  • 7
  • 15
  • 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. – kiran kumar May 29 '18 at 09: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. – Prasanna venkatesh May 29 '18 at 09:29
  • Unable to locate element: {"method":"xpath","selector":"//a[text()='Part Details']"} error occured. – kiran kumar May 29 '18 at 10:25
  • Give browser.sleep or proper wait commands after switching into the frame and try to execute – Prasanna venkatesh May 29 '18 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. – kiran kumar May 29 '18 at 10:51
  • Remove the double quote and try in the detailsDisplay
    frame.driver.switchTo.frame(detailsDisplay); this will work
    – Prasanna venkatesh May 29 '18 at 11:11
  • Also remove the quotes in this command also driver.switchTo.frame(content); – Prasanna venkatesh May 29 '18 at 11:19
  • switchto.farme() method takes arguments of type int, string and webelement. here we are passing it as String right. It doesn't workout giving double quotes. – kiran kumar May 29 '18 at 11:59
  • I have updated the question. – kiran kumar May 29 '18 at 12:22
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");