从 HTML 文本中获取特定值

Posted

技术标签:

【中文标题】从 HTML 文本中获取特定值【英文标题】:To get particular value from HTML text 【发布时间】:2020-01-02 08:22:31 【问题描述】:

我有一个场景,我需要从以 html 格式返回的文本中获取 patientIId 的值。 当我查询下面的 xpath driver.findElement(By.Xpath("//div[@name='DemographicsTab']//div[@class='control-xml'][contains(text(),'

*

请帮助我从上述文本中获取“PagePatientDemographicsIID”的值。 即我需要 value = "53"

提前致谢

【问题讨论】:

【参考方案1】:

如果您将以下内容添加到您的 X-Path,您应该能够获得所需的内容:/argument[@local-name="PagePatientDemographicsIID"]

这应该产生以下节点:<argument local-name="PagePatientDemographicsIID" value="53" />

【讨论】:

.getText() 也不会给他 53,因为它在 value 属性中【参考方案2】:

要从 HTML 中提取 PagePatientDemographicsIID 的值,即 53,您必须为 visibilityOfElementLocated() 引入 WebDriverWait,您可以使用以下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"));

【讨论】:

谢谢,但 getAttribute("value") 返回 null。 driver.findElement(By.Xpath("//div[@name='DemographicsTab']//div[@class='control-xml'][contains(text(),'" @HemaLatha 如果By.cssSelector("view[target-uri='DemographicsTab'][view$=GenericFrameTabberInnerContainer] argument[local-name=PagePatientDemographicsIID]")//view[@target-uri='DemographicsTab' and contains(@view, 'GenericFrameTabberInnerContainer')]//argument[@local-name='PagePatientDemographicsIID'] 唯一标识该元素,您能否在chrome-devtools 中交叉检查一次? 交叉检查,它不是识别元素,因为“view[target-uri='DemographicsTab'][view$=GenericFrameTabberInnerContainer] argument[local-name=PagePatientDemographicsIID]”是一个文本 @HemaLatha 95% 的可能性 web 元素在 <iframe> 内,您需要先切换到 iframe【参考方案3】:

更新 要从文本中获取价值,您可以使用正则表达式:

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

如果页面上只有一个元素:

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

如果您知道 PagePatientIID,就会获得价值:

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

如果有多个PagePatientIID,则获取所有:

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

【讨论】:

以上是关于从 HTML 文本中获取特定值的主要内容,如果未能解决你的问题,请参考以下文章

从html标签jquery获取特定文本[重复]

如何在 db2 查询中从字符串中获取特定值

C#中的变量无法从html文本框中获取值

从特定元素获取文本时出现 NullPointerException [重复]

Gatling :从 JSON 中获取特定值的条件

使用 JSON 从 API 中提取特定值的问题