如何使用带有 JavaScript 的 XPATH 选择与元素内部文本的一部分匹配的元素 [英] How to select an element that matches a part of the element's inner text using XPATH with JavaScript

查看:30
本文介绍了如何使用带有 JavaScript 的 XPATH 选择与元素内部文本的一部分匹配的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一些链接的 HTML 文档,我正在尝试使用 XPATH 选择包含 AIG 公司网站的链接.

 

这是HTML:

<头><link rel="stylesheet" href="style.css"/><身体><h1>你好,Plunker!</h1><div class="cmp-AboutMetadata-itemInner"><div class="cmp-AboutMetadata-itemTitle">网站</div><div class="cmp-AboutMetadata-itemCotent">

<script src="script.js"></script></html>

这是我的 JavaScript 代码 和 plunker:

var link = document.evaluate("//а[@class='cmp-CompanyLink' and contains(text(), 'website')]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);document.body.innerHTML += "<br/>结果:<br/>";document.body.innerHTML += link.singleNodeValue;如果(链接.单节点值){const 结果 = "<br/> 找到:" + link.singleNodeValue.textContent;document.body.innerHTML += 结果;}

我不确定这里的错误是什么.关于为什么链接为空的任何想法?如何获得正确的节点?

解决方案

您的 XPath 似乎包含一个西里尔字符a"(在//a 中):

https://www.fileformat.info/info/unicode/char/0430/index.htm

用普通的替换它就可以了.

替代:

//a[starts-with(.,"AIG")]/@href

I have an HTML document that contains some links and I'm trying to use XPATH to select the link containing the website for the company AIG.

 <a class="cmp-CompanyLink"
      href="http://www.aig.com/careers"
      target="_blank"
      rel="nofollow noopener"
      data-tn-link="redirect"
      data-tn-element="companyLink"
      >AIG website</a>

Here is the HTML:

<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <h1>Hello Plunker!</h1>
    <div class="cmp-AboutMetadata-itemInner">
      <div class="cmp-AboutMetadata-itemTitle">Website</div>
      <div class="cmp-AboutMetadata-itemCotent">
        <a
          class="cmp-CompanyLink"
          href="https://twitter.com/AIGinsurance"
          target="_blank"
          rel="nofollow noopener"
          data-tn-link="redirect"
          data-tn-element="companyLink"
          >Twitter</a
        >
        <br />
        <a
          class="cmp-CompanyLink"
          href="https://www.facebook.com/AIGInsurance"
          target="_blank"
          rel="nofollow noopener"
          data-tn-link="redirect"
          data-tn-element="companyLink"
          >Facebook</a
        >
        <br /><a
          class="cmp-CompanyLink"
          href="https://twitter.com/AIGinsurance"
          target="_blank"
          rel="nofollow noopener"
          data-tn-link="redirect"
          data-tn-element="companyLink"
          >Twitter</a
        ><br /><a
          class="cmp-CompanyLink"
          href="https://www.facebook.com/AIGInsurance/"
          target="_blank"
          rel="nofollow noopener"
          data-tn-link="redirect"
          data-tn-element="companyLink"
          >Facebook</a
        ><br /><a
          class="cmp-CompanyLink"
          href="https://www.linkedin.com/company/aig"
          target="_blank"
          rel="nofollow noopener"
          data-tn-link="redirect"
          data-tn-element="companyLink"
          >LinkedIn</a
        ><br /><a
          class="cmp-CompanyLink"
          href="https://www.instagram.com/aigrugby/"
          target="_blank"
          rel="nofollow noopener"
          data-tn-link="redirect"
          data-tn-element="companyLink"
          >Instagram</a
        ><br /><a
          class="cmp-CompanyLink"
          href="https://www.youtube.com/user/AIG"
          target="_blank"
          rel="nofollow noopener"
          data-tn-link="redirect"
          data-tn-element="companyLink"
          >YouTube</a
        ><br /><a
          class="cmp-CompanyLink"
          href="http://www.aig.com/careers"
          target="_blank"
          rel="nofollow noopener"
          data-tn-link="redirect"
          data-tn-element="companyLink"
          >AIG website</a
        ><br /><a
          data-tn-action-click="true"
          data-tn-element="less-link"
          href="#"
          >less</a
        >
      </div>
    </div>
    <script src="script.js"></script>
  </body>
</html>

Here is my JavaScript code and the plunker:

var link = document.evaluate("//а[@class='cmp-CompanyLink' and contains(text(), 'website')]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);

document.body.innerHTML += "<br />Result:<br />";

document.body.innerHTML += link.singleNodeValue;

if(link.singleNodeValue){
  const result = "<br /> found: " + link.singleNodeValue.textContent;
  document.body.innerHTML += result; 
}

I'm not sure what's the error here. Any ideas on why the link is null? How do I get the correct node?

解决方案

It seems your XPath contains a cyrillic character "a" ( in //a) :

https://www.fileformat.info/info/unicode/char/0430/index.htm

Just replace it with a normal one and it should work.

Alternative :

//a[starts-with(.,"AIG")]/@href

这篇关于如何使用带有 JavaScript 的 XPATH 选择与元素内部文本的一部分匹配的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆