Selenium WebDriver Java - access variables and values in html script

281 views
Skip to first unread message

elisabeth.ga...@gmail.com

unread,
Jul 14, 2015, 5:38:03 PM7/14/15
to seleniu...@googlegroups.com
Hi there!

I´m quite new to Selenium in general and I have a general and a more detailed question which I could not solve with google or stackoverflow etc. 
I hope you can help me.

I´m using Selenium WebDriver with Java.

I have an automated test written in Java which is working. Fine so far.
One of the html pages has a script on its page hardcoded. This script has some variables and objects.

My aim: I´d like to access all the variables and values of that particular page and get them into a separate file later.
My question: is that even possible with Selenium to get those values? And if yes, then how do I get them?

For example:
I wanted to start small and to access "name3" and its values.

<html>
...
<script id="id_name" type="application/json">{
"name" : "value",
"name2" : { "name3" : "value3"},
...

</script>

What I did until now:

      JavascriptExecutor js = (JavascriptExecutor) driver;

      Object value = js.executeScript("return document.name3;");

      System.out.println("name3:" + value);


value is null in this case. This is false, it should be a string.

What am I missing?

Thanks for any advice.

Xiang Dong

unread,
Jul 14, 2015, 7:12:54 PM7/14/15
to seleniu...@googlegroups.com
can you get those variable from Javascript, if yes, you can call those Javascript via webdriver to retrieve it back to your test case.

Google how to call Javascript method in WebDriver will help you.

--david


Date: Tue, 14 Jul 2015 06:54:13 -0700
From: elisabeth.ga...@gmail.com
To: seleniu...@googlegroups.com
Subject: [selenium-users] Selenium WebDriver Java - access variables and values in html script
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/0bde874f-d11e-4e2b-82fc-968c6a9be952%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

elisabeth.ga...@gmail.com

unread,
Jul 15, 2015, 9:47:43 AM7/15/15
to seleniu...@googlegroups.com
Hi David 

thank you for your answer.

I found out that it isn´t a javascript object but a JSON-Object. I can use executeScript() for executing a javascript script.

I´m using Webdriver, Java and OSX:

My thoughts on getting the values out of the JSON object are:
1) Getting the JSON object into my code via i.e. executeScript-Method
2) Parsing the JSON object with the json object model api

The result is still null..
What is the right approach to get the values? 

Best regards,
Eli
Reply all
Reply to author
Forward
0 new messages