-1

I have a selenium test in java that works fine on DEV environment using a function findElement(By.xpath("actual xpath")).getAttribute("actual attribute"), which is getting me a value of an input. I tried the same test run on PROD environment, but it couldn't find this attribute. For some reason the value of this input is not visible anywhere in the xpath. I know the input's id, so I wrote in my browser's console a simple javascript function document.getElementById("actual id").value and it returned the correct value that I need, so that information is sadly hidden from java, but not from javascript. Is there a way, how can I use this javascript function in my java code?

Here's what I tried:

String id = driver.findElement(By.xpath("//label[text()='First Name(s)']")).getAttribute("for");
JavascriptExecutor js = (JavascriptExecutor) driver;
String method = "\"return document.getElementById('" + id + "').value;\"";
Object name = js.executeScript(method);

As you can guess, it didn't work. Object name is returning just null. I'm sure that the id is correct, verified it by debug, so I had to do a mistake somewhere else.

DEV environment:

<input _ngcontent-eqp-c44 class="input-element ng-untouched ng-pristine ng-valid" ng-reflect-model="John" id="input_id_3301863451932101" type="text">

I need the value "John" from ng-reflect-model which is super easy on this environment, but

PROD environment:

<input _ngcontent-xjq-c2 class="input-element ng-untouched ng-pristine ng-valid" id="input_id_6695429395219272" type="text">

There you can see more of the HTML

there is just nothing I can use with java...

Ciri
  • 1
  • 1
  • 2
  • have you tried to find this element by id using WebDriver? driver.findElementById("yourId");? – Vault23 Sep 27 '19 at 11:26
  • @Vault23 I tried `driver.findElement(By.id(id));` to get it as a WebElement, but it doesn't include the value I'm looking for and I even tried to add `.getText()` at the end, but I should have known it wouldn't work since the doesn't have `innerHTML`. How stupid of me... – Ciri Sep 27 '19 at 11:50
  • try ele.getAttribute('value'). it may work. – Murthi Sep 27 '19 at 12:06
  • is that js working when you run in js-console in browser dev tools? – Alexey R. Sep 27 '19 at 12:13
  • Update the question with more of the outerHTML from the _PROD environment_ – undetected Selenium Sep 27 '19 at 12:13
  • @Ciri can you add full `HTML` of the site? – dafie Sep 27 '19 at 12:20
  • @DebanjanB I added a screenshot of the HTML from the PROD – Ciri Sep 27 '19 at 12:28
  • @dafie I added a screenshot with the whole HTML part, that belongs to the input. Hope it's enough – Ciri Sep 27 '19 at 12:30
  • @Ciri I suppose that the value is stored in different tag. – dafie Sep 27 '19 at 12:31
  • @AlexeyR. yes, `document.getElementById("id").value` will give me exactly what I need in js-console in browser dev tools – Ciri Sep 27 '19 at 12:31
  • @dafie I tried to find that value in HTML file using CTRL+F and it didn't find anything. I can see the value in the web application, I can get the value using javascript in console in browser dev tools... just that stupid HTML file won't give me what I need. – Ciri Sep 27 '19 at 12:39
  • @Ciri if it is displayed as a text it must be somewhere in html. – dafie Sep 27 '19 at 12:48

2 Answers2

0

I would suggest to try in script something like elem.getAttribute('ng-reflect-model')

IvanMikhalka
  • 184
  • 10
0

Finally found the answer! Thank you all for your cooperation!! I just had to change the last part of the code Object name = js.executeScript(method); with String name = js.executeScript(method).toString(); so if anybody has the same problem as me, you can get the value of an input in java using javascript and input's ID like this:

String id = driver.findElement(By.xpath("//label[text()='First Name(s)']")).getAttribute("for");
JavascriptExecutor js = (JavascriptExecutor) driver;
String method = "return document.getElementById('" + id + "').value;";
String name = js.executeScript(method).toString();
Ciri
  • 1
  • 1
  • 2