How to fix a error to write a text on a document-body html?

34 views Asked by At

Good morning everybody, I am writing for you to help me for a method that contains an error.

I am trying to automatize a test for writing a text on a document.body.innerHTML with the method executeJavascript but it's block in a code line. What can I do to fix this error :

"org.openqa.selenium.JavascriptException: SyntaxError: unexpected token: identifier".

Thanks for your help. Here is my part of the code:

public void publishCommentInFeedback(String commentTxt) {
    JLearnSelectorUtils
        .findElementByCssSelector(".comment-pane .dbcomment-add A").click();

    ActionUtils.switchToFrame(
        JLearnSelectorUtils.findElementByCssSelector(".new-comment-container IFRAME")
    );

    ActionUtils
        .executeJavascript("document.body.innerHTML = '" + commentTxt + "'");

    ActionUtils.switchToDefaultContent();
    JLearnSelectorUtils.findElementByCssSelector(".new-comment-container .btn").click();
    ActionUtils.waitUntilElementVisible(60, ".fulldisplay-footer .wysiwyg-editor");
}
1

There are 1 answers

0
pburgr On

Missing ; at the end of the script:

   ActionUtils.executeJavascript("document.body.innerHTML = '" + commentTxt + "'");

vs

   ActionUtils.executeJavascript("document.body.innerHTML = '" + commentTxt + "';");

A different approach to find the body element:

String desiredText = "Kevens";
String scr = "document.getElementsByTagName('body')[0].innerHTML = '" + desiredText + "';";
JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
javascriptExecutor.executeScript(scr);