3

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?

fool-dev
  • 7,671
  • 9
  • 40
  • 54
ali-myousefi
  • 822
  • 2
  • 11
  • 26

1 Answers1

4

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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875