Can't select an Iframe in selenium webdriver
Asked Answered
A

4

6

I am trying to select an Iframe by a class name but it's not working , I am trying with tagName it works but then when I tried to type within the element in the Iframe I couldn't, could you please help me here is my code:

webDriver driver.switchTo().frame( driver.findElement( By.className( "cke_wysiwyg_frame cke_reset" ) ) );
driver.findElement( By.xpath( "//body[contains(text(),'type here')]" ) ).sendKeys( "Testing" );

And here is the HTML in my webpage:

<div id="cke_534_contents" class="cke_contents cke_reset" role="presentation" style="height: 75px;">
   <span id="cke_586" class="cke_voice_label">Press ALT 0 for help</span>
   <iframe class="cke_wysiwyg_frame cke_reset" frameborder="0" src="" style="width: 100%; height: 100%;" aria-describedby="cke_586" tabindex="0" allowtransparency="true">
      <!DOCTYPE html>
      <html lang="en-gb" dir="ltr">
         <head>
         <body class="cke_editable cke_editable_themed cke_contents_ltr cke_show_borders" contenteditable="true" spellcheck="true">
            type here
         </body>
      </html>
   </iframe>
</div>
Abusive answered 8/9, 2014 at 9:37 Comment(1)
I don't use className all that often for finding elements but I'm pretty sure that it can't include any white=space in the parameter. Did you try using cssSlector instead? driver.findElement(By.CssSelector("cke_wysiwyg_frame.cke_reset"));Mcnary
A
11

the problem was solved by finding the iFrame by xpath

driver.switchTo().defaultContent();
driver.switchTo().frame( driver.findElement( By.xpath( iframeXpath ) ) );

and then return to the top window:

 driver.switchTo().defaultContent();
Abusive answered 12/9, 2014 at 13:52 Comment(0)
H
4

You cant select an iFrame using class.Check the webdriver documentation using : -

https://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/WebDriver.TargetLocator.html

  1. You can switch to iFrame by 'Name'/'id' attribute.

    driver.switchTo().frame("frame1");
    
  2. You can switch by frame index.

    driver.switchTo().frame(driver.findElements(By.tagName("iframe").get(0));
    

Now you have to check in the whole page how many iFrames are present ?? If its say : 3, use the 3rd iFrame always.

driver.switchTo().frame(driver.findElements(By.tagName("iframe").get(3));

I hope it helps.

Herrick answered 8/9, 2014 at 11:6 Comment(2)
Hi M, when i type the line above i don't get the get(); method "tagName("iframe").get(3));"Abusive
You need an extra missing bracket : driver.switchTo().frame(driver.findElements(By.tagName("iframe")).get(0));Holy
H
1

Yes, there was a mistake..Ok you can do one thing..Manually count how many iframe are there in the page, if its 3rd one whre you want to switch.

Directly specify the int value as doc says,

driver.switchTo().frame(index)

So your code can become something like this : -

driver.switchTo().frame(3);

And dont forget to get back to default Content.

driver.switchTo().defaultContent();

Please let me know if that works or not.

Herrick answered 8/9, 2014 at 13:23 Comment(0)
J
0

We can also get an element using locator to that iframe then switch to iframe using that element. more info here

// Store the web element
const iframe = driver.findElement(By.css('#modal > iframe'));

// Switch to the frame
await driver.switchTo().frame(iframe);
Jericajericho answered 12/12, 2022 at 11:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.