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?
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 pound selector (adding more on the left of your existing selector) depending on whether there are multiple matches for the parent container, etc.