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.
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
-
2Possible duplicate of How do I get particular textNode value of a DOM with SeleniumAlexey R.– Alexey R.2018年11月21日 14:23:19 +00:00Commented 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.Marcel Wilson– Marcel Wilson2018年11月21日 14:49:24 +00:00Commented Nov 21, 2018 at 14:49
1 Answer 1
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.
-
Ultimately, I used the same functions as you suggested above & it was working.Thanks though.Prasad_Joshi– Prasad_Joshi2018年11月26日 09:52:33 +00:00Commented Nov 26, 2018 at 9:52