edit content in Ck editor

24 views
Skip to first unread message

Riya kaur

unread,
Jul 16, 2025, 10:54:39 PMJul 16
to Selenium Users
In my automation, im not able to add text in ck editor element.
it not breaks the step but its not able to add content in ck editor.

Suriya Elamparithy

unread,
Jul 26, 2025, 2:22:29 AMJul 26
to Selenium Users
 When automating a CKEditor-based rich text editor using Selenium, simply using sendKeys() often does not work because the editor content is inside an iframe, and the text area is usually a contenteditable <body> or <div>.  

public class CKEditorExample {
    public static void main(String[] args) throws InterruptedException {
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();

        driver.get("https://ckeditor.com/ckeditor-4/demo/");  // or your own app
        Thread.sleep(3000); // Wait for editor to load

        // 1. Switch to the iframe
        WebElement iframe = driver.findElement(By.cssSelector(".cke_wysiwyg_frame"));
        driver.switchTo().frame(iframe);

        // 2. Locate the editable <body> and enter text
        WebElement body = driver.findElement(By.cssSelector("body"));
        body.clear();  // Optional: to clear existing content
        body.sendKeys("Hello, this is text entered in CKEditor!");

        // 3. Switch back to default content if needed
        driver.switchTo().defaultContent();

        Thread.sleep(3000);
        driver.quit();
    }
}

  If sendKeys() Still Doesn’t Work
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].innerHTML = 'This is inserted using JS';", body);
Reply all
Reply to author
Forward
0 new messages