0

I am facing the following issue, while executing javascript from the PhantomJS Driver using serenity framework.

 (PhantomJSDriver) driver).executeScript("document.getElementsById('whereOut')")

Error:

{"errorMessage":"'undefined' is not a function (evaluating 'document.getElementsById('whereOut')')","request":{"headers":{"Accept-Encoding":"gzip,deflate","Connection":"Keep-Alive","Content-Length":"69","Content-Type":"application/json; charset=utf-8","Host":"localhost:33819","User-Agent":"Apache-HttpClient/4.3 (java 1.5)"},"httpVersion":"1.1","method":"POST","post":"{\"script\":\"document.getElementsById(\u0027whereOut\u0027)\",\"args\":[]}","url":"/execute","urlParsed":{"anchor":"","query":"","file":"execute","directory":"/","path":"/execute","relative":"/execute","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/execute","queryKey":{},"chunks":["execute"]},"urlOriginal":"/session/b0246730-4435-11e6-b64c-7bf1e13a7577/execute"}} Command duration or timeout: 278 milliseconds

Also tried following code:

((JavascriptExecutor) driver).executeScript("document.getElementById('whereOut')")

above code returns a null value even if I add a return value as shown below:

((JavascriptExecutor) driver).executeScript("return document.activeElement")

Returns a null value.

Has anyone tried to change the css of a particular element using phantom js?

((JavascriptExecutor) driver).executeScript("document.getElementById('whereOut').className=''")

What I figured out is that while executing above line of code, I don't get the element using javascript execution while the same element is retrievable using pure selenium code with firefox browser.

P.S. I am using phantomJS 1.9.7 version along with serenity: 1.0.56.

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
Bilbo Baggins
  • 2,899
  • 10
  • 52
  • 77

1 Answers1

0

Try using executeAsyncScript with callback as below :-

String changedClassName = ((JavascriptExecutor) driver).executeAsyncScript("var callback = arguments[arguments.length - 1];var element = document.getElementById('whereOut');element.setAttribute('class', 'your new class name');callback(element.getAttribute('class'));");

Edited :- If you want to modify className of desire element then try using .setAttribute as below

WebElement element = driver.findElement(By.id("whereOut"));
String changedClassName = ((JavascriptExecutor) driver).executeAsyncScript("var callback = arguments[arguments.length - 1];arguments[0].setAttribute('class', arguments[1]);callback(arguments[0].getAttribute('class'));", element, "your new class name");

Hope it will help you...:)

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
  • Not working, I get this error [org.openqa.selenium.remote.RemoteWebElement@4fb0e071 -> unknown locator] – Bilbo Baggins Jul 11 '16 at 05:46
  • @BilboBaggins make sure first at your browser console `document.getElementById('whereOut')` returns something or not – Saurabh Gaur Jul 11 '16 at 05:49
  • if I do the same using selenium WebElement element = driver.findElementBy(By.id("whereOut")); or driver.findElementBy(By.cssClassName("whereOut")); (method for cssClass name may not be correctly named), I do get the desired div tag, but I can't remove its css using WebElement instance for that I have to execute the above (mentioned in question ) java script which is not working strange because the same thing works fine with pure selenium code, but I have to use the Serenity BDD framework – Bilbo Baggins Jul 11 '16 at 06:27
  • @BilboBaggins ok if you want to modify class name you can do this by javascript using `.setAttribute()`..see edited answer.. – Saurabh Gaur Jul 11 '16 at 06:39