简体   繁体   English

在 Selenium Webdriver 中定位 CK 编辑器并向其发送文本

[英]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).嗨,我正在尝试通过 Selenium Webdriver 代码(Java)为我的项目找到 CK 编辑器。 But Whenever I try to use SendKeys() method it is not working for me.但是每当我尝试使用 SendKeys() 方法时,它都对我不起作用。 Below is the screenshot of CK Editor and HTML code.下面是CK编辑器和HTML代码的截图。

在此处输入图片说明

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.请帮助我找到 CK 编辑器并向其发送文本。

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.我们在 Selenium WebDriver In Practice 一书的第 3 章中提供了与编辑器合作的技巧。

I think iframe is being searched based on cssSelector but i think it is supposed to be based on id?我认为 iframe 是基于 cssSelector 进行搜索的,但我认为它应该基于 id? which is scayt_8.这是 scayt_8。 Can you try with following code to fetch iframe instead of cssSelector:您可以尝试使用以下代码来获取 iframe 而不是 cssSelector:

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

Once you switch to iframe, try to locate webelement by paragraph tag name inside iframe, something like below:-切换到 iframe 后,尝试通过 iframe 中的段落标记名称定位 webelement,如下所示:-

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

Then try to send keys using this webelement:然后尝试使用此 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.这将帮助您在 CKeditor 中发送文本。 Try this.尝试这个。 It will work它会工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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