1

Using Selenium 4.8 in .NET 6, I have the following html structure to parse.

<ul class="search-results">
  <li>
    <a href=//to somewhere>
      <span class="book-desc">
       <div class="book-title">some title</div>
       <span class="book-author">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.

enter image description here

1 Answers1

1

class="book-author" belongs to span while class="book-title" 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"
Prophet
  • 32,350
  • 22
  • 54
  • 79
  • got an exception. Unable to locate element with.... – SlappingTheBass12 Jan 24 '23 at 17:05
  • Can you share a link? we are missing basic debugging details. – Prophet Jan 24 '23 at 17:06
  • {"invalid selector: Unable to locate an element with the xpath expression //li[.//span[@class='book-author' and(contains(text(),'Terry Goodkind'))] and .//div[@class='book-title' and(contains(text(),'Blood of the Fold'))]]] because of the following error:\nSyntaxError: Failed to execute 'evaluate' on 'Document': The string '//li[.//span[@class='book-author' and(contains(text(),'Terry Goodkind'))] and .//div[@class='book-title' and(contains(text(),'Blood of the Fold'))]]]' is not a valid XPath expression.\n (Session info: chrome=108.0.5359.126)"} – SlappingTheBass12 Jan 24 '23 at 17:09
  • Not the error trace, a link to the page you working on – Prophet Jan 24 '23 at 17:10
  • https://openlibrary.org/works/OL2010487W/Blood_of_the_fold added a picture too. Basically, I'm entering title and author in the header search field then parsing that dropdown response – SlappingTheBass12 Jan 24 '23 at 17:11
  • In the shared page no results appearing below the `ul class="search-results"` element. No elements with class name `class="book-desc"` etc – Prophet Jan 24 '23 at 17:16
  • it's in the popover of the main search in the navbar, not in any one page. – SlappingTheBass12 Jan 24 '23 at 17:17
  • You are right, it was extra `]`. Now I fixed it. Please see the updated answer – Prophet Jan 24 '23 at 17:56
  • I think I need to do case insensitive. The system wrote in "Blood of the Fold" and it found 0 probably because the title in the html is "Blood of the fold" – SlappingTheBass12 Jan 24 '23 at 18:12
  • Well, you will have to pass text exactly in the case sensitive way it appears in web elements. Alternatively you will have to use `translate` function as described here https://stackoverflow.com/questions/8474031/case-insensitive-xpath-contains-possible since Selenium supports xpath 1.0 only – Prophet Jan 24 '23 at 18:16