Hema Latha
Hema Latha

Reputation: 85

To get particular value from HTML text

I have a scenario where i need to fetch the value of patientIId from the text returned in the format of HTML. When i query the below xpath driver.findElement(By.Xpath("//div[@name='DemographicsTab']//div[@class='control-xml'][contains(text(),'< view view="SAC.UIConfig.GenericFrameTabberInnerCo')]

I get the below text

 "<view view="SAC.UIConfig.GenericFrameTabberInnerContainer" 
    target-uri="DemographicsTab" 
    tab-appearance=" AbsoluteView TabContainer "
    appearance-class=" AbsoluteView TabContainer " title="">
    <argument local-name="PagePatientIID" value="10032" />
    <argument local-name="Patient" value="10032" />
    <argument local-name="PagePatientDemographicsIID" value="53" />
    <argument local-name="Node" value="DemographicsTab" />
    <argument local-name="CurrentFrame" value="ContentFrame" /><argument local-name="CurrentViewTargetName" value="DemographicsTab" />
    </view>"

*

Kindly help me in getting the value of "PagePatientDemographicsIID" from the above text. i.e i need the value = "53"

Thanks In Advance

Upvotes: 0

Views: 210

Answers (3)

Sers
Sers

Reputation: 12255

Update To get value from text, you can use regex:

String tt = "<view view=\"SAC.UIConfig.GenericFrameTabberInnerContainer\" \n" +
        "    target-uri=\"DemographicsTab\" \n" +
        "    tab-appearance=\" AbsoluteView TabContainer \"\n" +
        "    appearance-class=\" AbsoluteView TabContainer \" title=\"\">\n" +
        "    <argument local-name=\"PagePatientIID\" value=\"10032\" />\n" +
        "    <argument local-name=\"Patient\" value=\"10032\" />\n" +
        "    <argument local-name=\"PagePatientDemographicsIID\" value=\"53\" />\n" +
        "    <argument local-name=\"Node\" value=\"DemographicsTab\" />\n" +
        "    <argument local-name=\"CurrentFrame\" value=\"ContentFrame\" /><argument local-name=\"CurrentViewTargetName\" value=\"DemographicsTab\" />\n" +
        "    </view>";
String pattern = "PagePatientDemographicsIID\"\\s+value=\"(.*)\"";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(tt);
System.out.println("PagePatientDemographicsIID value: " + m.group(1));

If there's only one element on the page:

String patientDemographicsIID = driver.findElement(By.id("PagePatientDemographicsIID")).getAttribute("value");

Get value if you know PagePatientIID:

String patientDemographicsIID = driver.findElement(By.xpath("//view[@target-uri='DemographicsTab' and .//*[@value='10032']]//*[@local-name='PagePatientDemographicsIID']")).getAttribute("value");

Get all PagePatientIID if there're more than one:

driver.findElements(By.id("PagePatientDemographicsIID"))
                .forEach(patient -> System.out.println(patient.getAttribute("value")));

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193338

To extract the value of PagePatientDemographicsIID i.e. 53 from the HTML you have to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategy:

  • cssSelector:

    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("view[target-uri='DemographicsTab'][view$=GenericFrameTabberInnerContainer] argument[local-name=PagePatientDemographicsIID]"))).getAttribute("value"));
    
  • xpath:

    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//view[@target-uri='DemographicsTab' and contains(@view, 'GenericFrameTabberInnerContainer')]//argument[@local-name='PagePatientDemographicsIID']"))).getAttribute("value"));
    

Upvotes: 0

npinti
npinti

Reputation: 52195

You should be able to get what you need if you add the following to your X-Path: /argument[@local-name="PagePatientDemographicsIID"].

This should yield the following node: <argument local-name="PagePatientDemographicsIID" value="53" />.

Upvotes: 1

Related Questions