0

I am working with a page source that runs Javascript to print text on the page. Here is a snippet of the source I'm speaking of:

var display_session = get_cookie("LastMRH_Session");
if(null != display_session) {
    document.getElementById("sessionDIV").innerHTML = '<BR>The session reference number: &nbsp;' + display_session + '<BR><BR>';
    document.getElementById("sessionDIV").style.visibility = "visible";
}

The page is displaying the value of the display session variable when it's not null but I want to know if there is a way I can utilize this variable in my Selenium WebDriver code. The function that uses this code in the Javascript does not return the display_session variable and I cannot alter the page source. I tried this based on the post here but it throws an exception.

JavascriptExecutor js = (JavascriptExecutor) driverRp; 
Object result = js.executeScript("return display_session");
System.out.println("sessionId = "+result);

Any suggestions?

Community
  • 1
  • 1
user2150250
  • 4,797
  • 11
  • 36
  • 44

3 Answers3

1

Figured it out. Needed to go after the cookie itself instead of messing with the Javascript variable.

Cookie cookie = driverRp.manage().getCookieNamed("LastMRH_Session");
String sessionId = cookie.getValue().toUpperCase();
System.out.println("sessionId = "+sessionId);

Found the solution at this post.

Community
  • 1
  • 1
user2150250
  • 4,797
  • 11
  • 36
  • 44
1

If the display_session variable is inside a function the you will not be able to access it unless it is at global scope.

If your intention is to read the value of a cookie, you can use the driver.manage().getCookieNamed(...) as an alternative to executing Javascript.

EDIT:

Just saw that you were able to figure out the same. Didn't see your answer when I posted. Glad it worked out.

shri046
  • 1,148
  • 11
  • 12
0

Try this:

js.executeScript('return get_cookie("LastMRH_Session")')
Vishal Aggarwal
  • 1,929
  • 1
  • 13
  • 23
  • Eclipse is giving me an error "invalid character constant" for that line of code. – user2150250 Nov 21 '14 at 20:08
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – gunr2171 Nov 21 '14 at 20:16