0

I found out that JavaScript is not working at all in my Selenium Tests in Java. I do not know why. Any suggestions?

((JavascriptExecutor) driver).executeScript("return arguments[0].innerText", driver.findElement(By.cssSelector("[id$=main:domainsCounter]")));

The javascript works fine in the console of the browser.

in console of browser

2
  • you probably won't need to run javascript to do that... just get the element and use element.getAttribute("innerHTML"); Commented Aug 27, 2024 at 20:35
  • We want to use the widgetVar from PrimeFaces. That is why we need JavaScript.
    – dragoness
    Commented Aug 28, 2024 at 7:23

2 Answers 2

0

Finally I found that is is possible and necessary to set options. Like this:

ChromeOptions chromeOptions = new ChromeOptions();
FirefoxOptions firefoxOptions = new FirefoxOptions();
EdgeOptions edgeOptions = new EdgeOptions();
chromeOptions.addArguments("--enable-javascript");
firefoxOptions.addArguments("--enable-javascript");
edgeOptions.addArguments("--enable-javascript");
try {
    chromeDriver = new RemoteWebDriver(
                new URL(getRemoteWebDriverUrl()),
                chromeOptions);
    firefoxDriver = new RemoteWebDriver(
                new URL(getRemoteWebDriverUrl()),
                firefoxOptions);
    edgeDriver = new RemoteWebDriver(
                new URL(getRemoteWebDriverUrl()),
                edgeOptions);
} catch (MalformedURLException e) {
    LOGGER.error("Test failed because url is wrong ", e);
}
-1

When you search the executeScript method, you will find this:

Executes JavaScript in the context of the currently selected frame or window. The script fragment provided will be executed as the body of an anonymous function. Within the script, use document to refer to the current document. Note that local variables will not be available once the script has finished executing, though global variables will persist.

If the script has a return value (i.e. if the script contains a return statement), then the following steps will be taken:

  • For an HTML element, this method returns a WebElement
  • For a decimal, a Double is returned
  • For a non-decimal number, a Long is returned
  • For a boolean, a Boolean is returned
  • For all other cases, a String is returned.
  • For an array, return a List<Object> with each object following the rules above. We support nested lists.
  • For a map, return a Map<String, Object> with values following the rules above.

Unless the value is null or there is no return value, in which null is returned

Arguments must be a number, a boolean, a String, WebElement, or a List of any combination of the above. An exception will be thrown if the arguments do not meet these criteria. The arguments will be made available to the JavaScript via the "arguments" magic variable, as if the function were called via "Function.apply".

At this point, the reason may be the selector you used didn't find the element at all. Why not just pass the selector string.

((JavascriptExecutor) driver).executeScript("return document.querySelector(arguments[0]).innerText", "#main:domainsCounter"));
1
  • This is off topic. The point was: For using JavaScript in Selenium tests, the option "--enable-javascript" has to be set.
    – dragoness
    Commented Aug 29, 2024 at 14:57

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.