简体   繁体   中英

Locating CK Editor and Sending text to it in Selenium Webdriver

Hi I am trying to locate CK editor for my project through Selenium Webdriver code(Java). But Whenever I try to use SendKeys() method it is not working for me. Below is the screenshot of CK Editor and HTML code.

在此处输入图片说明

And below is the code,

if(driver.findElement(By.cssSelector("iframe#scayt_8")).isEnabled())
{
  WebElement iframe = driver.findElement(By.cssSelector("iframe#scayt_8"));  
  System.out.println("Frame Enabled");
  if(driver.findElement(By.xpath("//iframe[@id = 'scayt_8']")).isDisplayed())           
  {           
    System.out.println("Frame Displayed");
    driver.switchTo().frame(iframe);
    iframe.clear();
    System.out.println("Clicking frame");
    iframe.click();
    iframe.sendKeys("Hello!!");
  }
}

Please help me to locate CK Editor and to Send text to it.

You probably need to switch to the inline frame to locate it.

WebElement editorFrame = driver.findElement(By.id("scayt_8"));

driver.switchTo().frame(editorFrame);

WebElement body = driver.findElement(By.tagName("body"));

body.clear(); 
body.sendKeys("some text");

We provide techniques for working with editors in chapter 3 of our book Selenium WebDriver In Practice.

I think iframe is being searched based on cssSelector but i think it is supposed to be based on id? which is scayt_8. Can you try with following code to fetch iframe instead of cssSelector:

driver.FindElement(By.TagName("iframe"))

Once you switch to iframe, try to locate webelement by paragraph tag name inside iframe, something like below:-

 WebElement body=driver.findElement(By.tagName("p"));

Then try to send keys using this webelement:

body.sendKeys("Hello!!");
WebElement iframe = driver.findElement(By.tagName("iframe")); driver.switchTo().frame(iframe); 
WebElement tinymce = driver.findElement(By.tagName("body")); 
tinymce.clear(); 
tinymce.sendKeys("Hello, ckeditor!");;


This will help you to send text in CKeditor. Try this. It will work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM