2014-02-11 101 views
5

我在Eclipse中使用Selenium WebDriver,我試圖插入文本到網頁上的tinymce框。這是我正在使用的代碼。無法發送鍵()到TinyMCE與硒WebDriver

//find tinymce iframe 
editorFrame = driver.findElement(By.cssSelector(".col_right iframe")); 

//switch to iframe 
driver.switchTo().frame(editorFrame); 
driver.findElement(By.className("mceContentBody")).sendKeys("YOOOO"); 
driver.switchTo().defaultContent(); 

光標在編輯器內閃爍,但沒有發送文本。 我也嘗試過這種輕微的變化,沒有任何運氣。

//find tinymce iframe 
editorFrame = driver.findElement(By.cssSelector(".col_right iframe")); 

//switch to iframe 
driver.switchTo().frame(editorFrame); 
WebElement body = driver.findElement(By.className("mceContentBody")); 
body.sendKeys("YOOOO"); 
driver.switchTo().defaultContent(); 
+0

看到這個問題:http://stackoverflow.com/questions/21684236/how-to-input-text-into-tincemce-editior-使用selenium-webdriver – Richard

+0

最後,一些工作!謝謝:)無論如何選擇你的評論作爲答案? – Sean

+0

我可以添加它作爲答案,但我寧願你upvote你在另一個問題上使用的答案。 – Richard

回答

5

是的,正如Richard所說,這是How to input text into tinceMCE editior using selenium/webdriver的重複。

針對您的特殊代碼,我建議

  • 嘗試不同的定位爲mceContentBody,e.g使用By.cssSelector(".mceContentBody")By.cssSelector("body")

  • 發送鍵之前,首先單擊體。

driver.findElement(By.tagName("body")).click().sendKeys("YOOOO"); 
  • 設置的innerHTML
inputWebDriver.switchTo().frame("input-data_ifr"); 
WebElement element = inputWebDriver.findElement(By.cssSelector("body")); 
(JavascriptExecutor)driver.executeScript("arguments[0].innerHTML = '<h1>Set text using innerHTML</h1>'", element); 
  • TinyMCE中使用的原生API
// no need to switch iframe 
(JavascriptExecutor)driver.executeScript("tinyMCE.activeEditor.setContent('<h1>Native API text</h1> TinyMCE')"); 

延伸閱讀:Test WYSIWYG editors using Selenium WebDriver

+0

我實際上嘗試點擊之前,並沒有工作。與JavaScript執行者的方法和設置內部的HTML在其他職位工作。謝謝 – Sean

+0

如何讓這個JS代碼爲'redactor-editor'工作?我已經嘗試過'driver.findElement(By.className(「redactor-editor).sendKeys(」hello world「));' – paul

+0

如何讓這個JS代碼適用於'redactor-editor'。它的API是[here] (http://imperavi.com/redactor/docs/api/)?? – paul

1

做到這一點(C#)

yourWebDriver.ExecuteScript(string.Format("tinyMCE.getInstanceById('{0}').setContent('{1}');",yourWebElement.GetAttribute("id"), "hello world"));