Kiril
Kiril

Reputation: 40345

How to select an element that matches a part of the element's inner text using XPATH with JavaScript

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?

Upvotes: 0

Views: 300

Answers (2)

E.Wiest
E.Wiest

Reputation: 5905

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

Upvotes: 1

Jack Fleeting
Jack Fleeting

Reputation: 24930

Try using

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

Upvotes: 1

Related Questions