Dima Zeuge
Dima Zeuge

Reputation: 99

How to identify and switch to the frame in selenium webdriver when frame does not have id and title

Can anyone tell me how I can identify and switch to the iframe which hasn't title and id. I need to get into this frame in order to find another element in this frame for sending some text.

<div class="sceditor-container" style="width: 1359px; height: 300px;">
  <div class="sceditor-toolbar">
  <iframe frameborder="0" src="javascript:false" style="width: 1349px; height: 261px;">
     <!DOCTYPE html>
     <html style="height:100%">
       <head>
             <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
             <link href="" type="text/css" rel="stylesheet">
       </head>
      <body contenteditable="true" style="height:88%">
        <div> </div>
      </body>
     </html>
 </iframe>

For this I wrote code:

WebDriver.SwitchTo().Frame(WebDriver.FindElement(By.TagName("iframe")));
WebDriver.FindElement(By.XPath("html/body")).SendKeys("text");
WebDriver.SwitchTo().DefaultContent();

But WebDriver can't find iframe element and as a result can't send text value into iframe element.

Upvotes: 5

Views: 16226

Answers (2)

MarkyMarksFunkyBunch
MarkyMarksFunkyBunch

Reputation: 1200

I used ChromeDriver for a while but recently switched to phantomjs as I was having problems with ChromeDriver hanging inexplicably. phantomjs (if my research is right) works almost exclusively via Javascript by (this is a bit more of a guess than hard facts) injection. So, how's that relevant? Well I discovered that you can run your own JS against the DOM of a page you've loaded. So I threw your source into https://www.freeformatter.com/xpath-tester.html#ad-output and tried to run some xpath against it and it threw up. After closing your meta and link tags with a forward slash, it and then adding closing div tags for the opening pair, it then evaluated my xpath as expected.
Can you correct these or is this source external to you? If you can, I'd do so. Then try running a Jscript using

driver.ExecuteScript("Your javascript here..");  

Put the code to set the value of your div object in the script. I don't have a sample handy but use an appropriate selector with

driver.querySelector("tag.className..").innerHTML = "Your Keys";

In playing around with this using the Console in chrome, I was still having issues. But it's my strong suspicion that malformed HTML is your culprit. If you've solved this I'd love to see the answer. Cheers!

Upvotes: 0

Rupesh Shinde
Rupesh Shinde

Reputation: 1956

To click and sendkeys use below code

WebElement loc=driver.findElement(By.XPath("html/body"));
 Actions a= new Actions(driver);
 a.click(loc).sendKeys("emailBody").perform();

To set the text in the iframe element you will have to first copy the text from the Body tag

WebElement emailBody = driver.findElement(by.xpath("/html/body"));
emailBody.getText();

Once Text is copied into emailBody, you can use it with element inside frame as below

WebDriver.FindElement(By.XPath("html/body")).SendKeys(emailBody);

Try below code to switch to frame and see what happens

driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(@src,'javascript:false')]")));

Also you can try to access frame using its index, try

driver.switchTo().frame(0);

Upvotes: 5

Related Questions