how do I get text value outside of any tag in html, through selenium in java



  • 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



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



Suggested Topics