0

I have text,which is not enclosed inside any of the HTML tag but is wrapped in between two HTML tag as shown in below image, how do I get the value 38 in this case, through selenium in java.

enter image description here

below is my code, which I tried but it's only giving the label value of <b> attribute and not the desired value i.e. 38 in this case

String ageAtAdmission=driver.findElement(By.xpath("//a[@name='profile']//b[4]")).getAttribute("innerHTML");
 AR_Utils.logMsg("Age at Admission "+ageAtAdmission);

OUTPUT : Age at Admisson Age at Admission

asked Nov 21, 2018 at 13:21
2
  • 2
    Possible duplicate of How do I get particular textNode value of a DOM with Selenium Commented Nov 21, 2018 at 14:23
  • You have to remember that the text you're looking for IS enclosed in an HTML tag. It might be the <body> or even in the <HTML> tag. Find that element, then you should be able to get the text attribute. Commented Nov 21, 2018 at 14:49

1 Answer 1

2

If you were to get the text of the parent anchor (//a[@name="profile"]/text()) you would find that the text you are seeking is contained within the string that's returned.

You'd get a string that contains something like

DEGREE : Age at Admission : 38 : PA Candidate :

The number of spaces between text entries could be different, but if you used a standard string library function to remove any spaces, you'd then have a string containing the following:

DEGREE:AgeatAdmission:38:PACandidate:

Since age at admission and PA Candidate are static text, you can then use the standard substring functions to strip off everything before your target text (38) and everything after. Or you could simply use the standard string split function to turn your string into an array. In C# or Java it would probably look something like string[] mystringarray = String.Split(':', mystring);

Then you find the index of the element with value "AgeatAdmission", go to the next element, and you have your value.

answered Nov 23, 2018 at 13:15
1
  • Ultimately, I used the same functions as you suggested above & it was working.Thanks though. Commented Nov 26, 2018 at 9:52

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.