Skip to content Skip to sidebar Skip to footer

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.

Solution 1:

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

Solution 2:

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

Post a Comment for "How To Select An Element That Matches A Part Of The Element's Inner Text Using Xpath With Javascript"