0

I want to get the urls from google results.

This xpath works perfectly BUT there is a problem:

//div[@class='r']/a

Google shows some results from featured snippets and People Also Ask (https://www.google.com/search?q=people+also+ask+example) and that xpath gets all those url results.

I realized there is a parent div with "g" class, and only the People Also Ask div has the class "kno-kp".

So the solution is easy:

Select every div class that contains "g" and not contains "kno-kp"

google results

This is my xpath but it doesn't work:

//div[contains(@class,'g') and not(contains(@class,'kno-kp'))]//div[@class='r']/a

Thank you for your help!

ou_ryperd
  • 2,037
  • 2
  • 18
  • 23
Alex Güemez
  • 27
  • 1
  • 5
  • `//div[@class='g']//div[@class='r']/a` only this xpath gave me 8 matching urls – Dev Sep 10 '20 at 05:07
  • Do you want the URLs that does't contains 'People Also Ask'? I have accessed the [URL](https://www.google.com/search?q=people+also+ask+example) from India and the div with class 'g' does't contain 'kno-kp' at all. – Dilip Meghwal Sep 10 '20 at 05:10
  • @AlexGüemez What was this url `https://www.google.com/search?q=people+also+ask+example` all about? – undetected Selenium Sep 10 '20 at 07:28

1 Answers1

0

I think it should fix your problem:

//div[@class='r'][count(ancestor::*[contains(concat(' ', @class, ' '), ' g ')])=1]

Useful info about xpath above: here