Is there any equivalent to selenium FindElements By.XPath in chrome extension java script?

Tags: , , , ,



I have some experience in Selenium, using some code as below to locate elements using XPath;

var btns = driver.FindElements(By.XPath(@"//button[(@type='submit')]"));

I recently started to develop an Chrome extension, and I could not find any similar way to locate elements using easier way similar to XPath.

I’m having many complex queries developed in selenium, using the XPath methods. I just want to do the same in Chrome Extension to locate elements.

I could only locate element in Chrome Extension content script as the method below;

const contents = document.getElementById('contents');

Can anyone please suggest me the best way to convert XPath to Chrome Extension content script without many modifications, and some way similar to XPath in Selenium? (My XPath statements are complex a bit)

Answer

You need to use evaluate like this:

document.evaluate("//xpath/here", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;


Source: stackoverflow