Reputation: 247
I'm trying to access to iFrame
<iframe id="frame1" style="width: 100%; height: 100%; display: block;" tabindex="100">
<html>
<head xmlns="http://www.w3.org/1999/xhtml">
<body id="tiny1" contenteditable="true" onload="window.parent.get('frame1').onLoad.dispatch();">
<p/>
</body>
</html>
</iframe>
And I want to sendKeys() to the body with id=tinymce. But when I tried to switchTo().frame , it does not work.
My Java code:
public void enterArea(String object, String content){
String driverWindows = driver.toString();
driver.switchTo().frame(selenium.driver.findElement(By.xpath("//*[@id='frame1']")));
String driverIFrame = driver.toString();
WebElement contentTextArea = (new WebDriverWait(driver, 3))
.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='tiny1']/p")));
contentTextArea.sendKeys(content);
driver.switchTo().defaultContent();
}
The 2 driverWindows
and driverIFrame
always return the same string, it means the web driver
has not switched, right?
Could you help me access to elements? Please do let me know if require any further details. Thank you.
Upvotes: 1
Views: 1088
Reputation: 3021
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@id='frame1']")));
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.tagName("body")).sendKeys("testing");
driver.switchTo().defaultContent();
Another option using javascript
Do not switch into the iframe if you are using javascript
((JavascriptExecutor)driver).executeScript("tinyMCE.activeEditor.setContent('testing');");
I tested the above code with timymce editors it was working fine
EDIT :
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@id='frame1']")));
//Do not use path body/p you need to send text to body tag
(new WebDriverWait(driver, 10))
.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//body[@id='tiny1']")));
driver.findElement(By.xpath("//body[@id='tiny1']")).sendKeys("testing");
driver.switchTo().defaultContent();
Hope this helps you..Kindly get back if you have any queries
Upvotes: 1