Using Selenium 4.8 in .NET 6, I have the following html structure to parse.
<ul >
<li>
<a href=//to somewhere>
<span >
<div >some title</div>
<span >some author</span>
</span>
</a>
</li>
</ul>
I need to find and click on the right li where the book-title matches my variable input (ideally ignore sentence case too) AND the book author also matches my variable input. So far I'm not getting that xpath syntax correct. I've tried different variations of something along these lines:
var matchingBooks = driver.FindElements(By.XPath($"//li[.//span[@class='book-author' and text()='{b.Authors}' and @class='book-title' and text()='{b.Title}']]"));
then I check if matchingBooks has a length before clicking on the first element. But matchingBooks is always coming back as 0.
CodePudding user response:
belongs to
span
while belongs to
div
child element.
Also it cane be extra spaces additionally to the text, so it's better to use contains
instead of exact equals
validation.
So, instead of "//li[.//span[@class='book-author' and text()='{b.Authors}' and @class='book-title' and text()='{b.Title}']]"
please try this:
"//li[.//span[@class='book-author' and(contains(text(),'{b.Authors}'))] and .//div[@class='book-title' and(contains(text(),'{b.Title}'))]]"
UPD
The following XPath should work. This is a example specific XPath I tried and it worked "//li[.//span[@class='book-author' and(contains(text(),'anima'))] and .//div[@class='book-title' and(contains(text(),'Coloring'))]]"
for blood of the fold
search input.
Also, I guess you should click on a
element inside the li
, not on the li
itself. So, it's try to click the following element:
"//li[.//span[@class='book-author' and(contains(text(),'{b.Authors}'))] and .//div[@class='book-title' and(contains(text(),'{b.Title}'))]]//a"