【问题标题】:How to get text from xhtml如何从 xhtml 中获取文本
【发布时间】:2017-10-04 21:05:14
【问题描述】:

<div>
  <fieldset>
    <legend class="lheader">Section Information:</legend>
    <span id="lblSectionInfo">
        Name:
        <font style="font-weight:normal">rr</font>
        <br>
        Type:
        <font style="font-weight:normal">Section Type     </font>
        Section List:
        <font style="font-weight:normal">	Yes       </font>
        Status:
        <font style="font-weight:normal">
        Section:
        <font style="font-weight:normal">Section Condition</font>
        <br>
    </span>
  </fieldset>
</div>

我必须从屏幕的上述部分信息部分获取文本。 我试过getText(),它返回空白行,试过getAttribute("innerText"),它返回N/A,试过getAttribute("innerHTML"),它返回N/A

不确定如何获得完整的文本或单独的测试,例如部分名称 应该返回文本部分“RR”等。我使用的 Xpath 是正确的。 非常感谢任何帮助。 仅供参考,我正在使用下面的 Xpath 来获取文本。

//div[@id = 'TestView5']//span[@id = 'lblSectionInfo']

我的 Xpath 是正确的,因为当 Selenium IDE 使用它时,我能够突出显示该部分的完整文本。

【问题讨论】:

  • HTML 中不存在只读元素。请完整显示声明
  • 你要传递什么给getText()?你的xpath 选择器是什么?
  • 希望对您有所帮助w3schools.com/xml/dom_element.asp
  • 发布您正在使用的实际代码。
  • 该示例不是格式良好的 XML。可能导致的 N/A 是由某处的错误处理程序引起的。

标签: html selenium selenium-webdriver xhtml


【解决方案1】:

这些字段很难分开,因为它们在同一个 span 节点下。我可以看到两种解决方法。
1.使用&lt;font&gt;节点的索引作为锚点。

Name: "//span[@id='lblSectionInfo']/font[1]"
Type: "//span[@id='lblSectionInfo']/font[2]"
Section List: "//span[@id='lblSectionInfo']/font[3]"


2. 使用 JavaScript 查找文本节点获取字段名称,然后使用 Selenium 查找 &lt;font&gt; 节点获取其值。最后,将它们映射在一起。

function getTextNode(rootNode) {
    var nodes = rootNode.childNodes;
    var fieldNames = [];
    var count=0;
    for (var i = 0; i < nodes.length; i++) {
        if ((nodes[i].nodeType == Node.TEXT_NODE)) {
            if(nodes[i].textContent.trim().indexOf(':')>0) {
                let text = nodes[i].textContent.trim();
                fieldNames[count] = text.substring(0,text.length-1);
            }           
        }
    }
    return fieldNames;
}

【讨论】:

  • 第一个解决方法非常适合我。 Thnx Buaban。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-18
  • 2021-08-17
  • 1970-01-01
  • 1970-01-01
  • 2016-10-14
相关资源
最近更新 更多