如何使用带有JavaScript的XPATH选择与元素内部文本的一部分匹配的元素

问题描述 投票:0回答:2

我有一个HTML文档,其中包含一些链接,并且我试图使用XPATH选择包含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>

这里是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>

这是我的JavaScript代码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; 
}

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

javascript dom xpath
2个回答
0
投票

尝试使用

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

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

document.body.innerHTML += link;

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

0
投票

似乎您的XPath包含西里尔字符“ a”(在// a中:

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

只需用普通的替换它,它就可以工作。

替代:

//a[starts-with(.,"AIG")]/@href
© www.soinside.com 2019 - 2024. All rights reserved.