# How to capture an injected value in RF using Javascript

**URL:** <https://forum.robotframework.org/t/how-to-capture-an-injected-value-in-rf-using-javascript/4788>\
**Category:** Libraries\
**Created:** [5 October 2022 19:06 UTC](https://forum.robotframework.org/t/how-to-capture-an-injected-value-in-rf-using-javascript/4788 "2022-10-05T19:06:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bweston](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/bweston/32/1298_2.png) [@bweston](https://forum.robotframework.org/u/bweston)\
**Post date:** [5 October 2022 19:06 UTC](https://forum.robotframework.org/t/how-to-capture-an-injected-value-in-rf-using-javascript/4788/1 "2022-10-05T19:06:08Z")

</div>

I am not sure if this is the correct area for this post , so let me know if I need to move it. I am using RF for testing, Selenium Library and when needed Javascript ( I am beginning to learn).

My goal is to compare two date values. One value is Current Date and the other value is a date that is injected into the DOM when user changes a value. We are using angular for UI. The current code fails because it is comparing current date to //input[@ng-reflect-value which produces an end result of NULL so the method fails.

I do not have to use Javascript but it seems that was the best solution.

Work Date should be today

```
Click Element ${WORKDATE}
${date}= Get Current Date
${workdate}= Execute Javascript document.getElementById("class") **//input[@ng-reflect-value]**;
Should Be Equal ${date} ${workdate}

```

The date underlined below is not static and will change when a user changes another elements (Work Date) value in the UI. I do not want to capture the time, just the date.

 ![image](https://europe1.discourse-cdn.com/flex005/uploads/robotframework1/original/2X/3/3cff20cd5bd78933f3616ac91fbb37a203bb1f89.png)

Error Message

 ![image](https://europe1.discourse-cdn.com/flex005/uploads/robotframework1/original/2X/1/11532cc7a576a2d388ffd9e94923798a8c39abd3.png)

---

<div class="post-metadata">

**Author:** ![rasjani](https://avatars.discourse-cdn.com/v4/letter/r/858c86/32.png) [@rasjani](https://forum.robotframework.org/u/rasjani)\
**Post date:** [5 October 2022 19:10 UTC](https://forum.robotframework.org/t/how-to-capture-an-injected-value-in-rf-using-javascript/4788/2 "2022-10-05T19:10:57Z")

</div>

> [@bweston](#):
>
> `${workdate}= Execute Javascript document.getElementById("class") **//input[@ng-reflect-value]**;`

too drunk to debug anything like this at the moment but …

how about adding “return” to to execute javascript like like:

```python
${workdate}= Execute Javascript return document.getElementById("class") **//input[@ng-reflect-value]**;

```

that might help…

---

<div class="post-metadata">

**Author:** ![bweston](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/bweston/32/1298_2.png) [@bweston](https://forum.robotframework.org/u/bweston)\
**Post date:** [5 October 2022 19:18 UTC](https://forum.robotframework.org/t/how-to-capture-an-injected-value-in-rf-using-javascript/4788/3 "2022-10-05T19:18:17Z")

</div>

No. I get the same results. I believe I need to do something in this part of the code so that I can capture the value.

`//input[@ng-reflect-value]**;`

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [6 October 2022 03:58 UTC](https://forum.robotframework.org/t/how-to-capture-an-injected-value-in-rf-using-javascript/4788/4 "2022-10-06T03:58:43Z")

</div>

Hi Barry,

Not being familiar with **ng-reflect-value** or any ng-reflect type attributes, I duck duck go’ed it (google’d it) to find out what the html element you are hitting would look like, I found an example but also noted there was a warning:

> [FYI you should not rely on `ng-reflect-*` since angular add these attributes only for debugging purpose](https://stackoverflow.com/questions/52241762/how-do-i-capture-the-value-of-ng-reflect-value-to-test-against-its-expected-valu)

Based on your test case and the html from the link above, I think what you want is [Get Element Attribute](https://robotframework.org/SeleniumLibrary/SeleniumLibrary.html#Get%20Element%20Attribute)

Probably something like this:

```python
${workdate}= Get Element Attribute //input[@ng-reflect-value] ng-reflect-value

```

I think the problem you’re encountering is that `document.getElementById` is returning a dom object to the browser and then the browser not being able to return an object and not having a default formatter is returning `None` rather than a string representation of the object.

You `Should Be Equal ` is failing as expected as `${workdate}` is `None` which isn’t a date or formatted date string.

Hope this is helpful,

Dave.

---

<div class="post-metadata">

**Author:** ![bweston](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/bweston/32/1298_2.png) [@bweston](https://forum.robotframework.org/u/bweston)\
**Post date:** [10 October 2022 14:49 UTC](https://forum.robotframework.org/t/how-to-capture-an-injected-value-in-rf-using-javascript/4788/5 "2022-10-10T14:49:09Z")

</div>

I will test your suggestion. I do have a related issue that I hope someone can assist, which is why I am using this same post.

In order to get a current date without time I decided to use this code:  
Execute Javascript new Date().toLocaleDateString()

When the method is run in console, I get a correct return:  
 ![image](https://europe1.discourse-cdn.com/flex005/uploads/robotframework1/original/2X/d/d3456eee9248e977a0fd0d5fecf83334fa671cbb.png)

I have two issues to resolve: I want to store the returned date in a variable but have not figured out how to do this and when this method is run through Robot Framework the return is NONE and not the expected output of the date as seen in the above image.

Log file:

 ![image](https://europe1.discourse-cdn.com/flex005/uploads/robotframework1/original/2X/4/41942279941e62874544bd985f0de6164709289f.png)

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [10 October 2022 22:10 UTC](https://forum.robotframework.org/t/how-to-capture-an-injected-value-in-rf-using-javascript/4788/6 "2022-10-10T22:10:55Z")

</div>

Hi Barry,

I gather this question relates to the `${date}= Get Current Date` line from your original question?

It looks like you are missing the return for the javascript:

```python
${mydate}= Execute Javascript return New Date().toLocalDateString()

```

I would suggest you stick with robot framework to create the date that you want to compare against, it’ll probably be a little faster as it only requires builtin keywords, plus you can then reuse the keyword on other projects that don’t use selenium

Something like this:

```python
***Keywords***
Get Current Date
	@{darr}=	Get Time	year month day
	${date}= Set Variable ${darr}[2]/${darr}[1]/${darr}[0]
	[Return] ${date}

```

 ![Screen Shot 2022-10-11 at 8.10.19 am](https://europe1.discourse-cdn.com/flex005/uploads/robotframework1/original/2X/7/747e2c292977b859add519de7991c35829278c1a.png)

Hope that helps,

Dave.

---

<div class="post-metadata">

**Author:** ![bweston](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/bweston/32/1298_2.png) [@bweston](https://forum.robotframework.org/u/bweston)\
**Post date:** [11 October 2022 18:31 UTC](https://forum.robotframework.org/t/how-to-capture-an-injected-value-in-rf-using-javascript/4788/7 "2022-10-11T18:31:41Z")

</div>

This is perfect and you are correct I can repurpose this across my project.

This resolved the first part of my question, which works well with the code you provided for the solution I desired.

${workdate}= Get Element Attribute //input[@ng-reflect-value] ng-reflect-value  
${newworkdate}= Convert Date ${workdate} result\_format=%Y/%m/%d
