If I want to locate only ‘grape’, what should I do? I tried using document.querySelector, but it doesn’t work. I don’t know how to locate html tags that don’t have an ID or class.
<h2 class="fruits">
<ul>
<li>apple</li>
<li>grape</li>
</ul>
</h2>
2
Answers
document.evaluate() lets you use xpath. The xpath
//*[.='grape']
should find elements with the text ‘grape’.This syntax is a bit ugly.
returns the first node matching the xpath.
This can solve your problem :