简体   繁体   中英

Execute JavaScript querySelectorAll on especial element using Selenium

According to this question I want find an element in my JavaScript code by some thing like this:

     IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
     string count = 
         js.ExecuteScript(
         "return document.querySelectorAll('ul.tag-list > li.tag-item').length")
         .ToString();

And It works correctly. But I need run my JavaScript code in the special piece of the page not the whole of a document. Is it anyway?

Yes, you can use querySelectorAll on an element as well. So select the element (via querySelector or getElementById ), then use querySelectorAll on it:

return document.querySelector('selector for the area of the page').querySelectorAll('ul.tag-list > li.tag-item').length;

You may also be able to do it with one compound selector (adding more on the left of your existing selector) depending on whether there are multiple matches for the parent container, etc.

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