-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...

12
  • have you tried to find this element by id using WebDriver? driver.findElementById("yourId");? Commented Sep 27, 2019 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 <input> doesn't have innerHTML. How stupid of me... Commented Sep 27, 2019 at 11:50
  • try ele.getAttribute('value'). it may work. Commented Sep 27, 2019 at 12:06
  • is that js working when you run in js-console in browser dev tools? Commented Sep 27, 2019 at 12:13
  • Update the question with more of the outerHTML from the PROD environment Commented Sep 27, 2019 at 12:13

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

Comments

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();

2 Comments

You shouldn't cast js.executeScript(method).toString() to String since toString() returns a String object
@AlexeyR. good point, I'll edit the answer. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.