简体   繁体   中英

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

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)

You need to use evaluate like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM