selenium
selenium

Reputation: 55

How to locate links from Google results

Scenario:

  1. open "google.co.in".
  2. click in the search input box.
  3. type something.
  4. click Enter.
  5. get the text of all links.

The xpaths of some links are:

All the xpaths have the same pattern. the third div needs to be incremented by 1 to get the next xpath. I've read somewhere that in the scenarios like this generic xpath can be used. According to his suggestion, the xpath will be ".//*[@id='rso']/div[2]/div/div/div/div/h3/a". just removed the predicate of the third div. This is not working. Is this the way to locate elements?

Upvotes: 0

Views: 3153

Answers (1)

Andersson
Andersson

Reputation: 52665

You can try below XPath to fetch all result links:

//div[@class="g"]//h3/a

If you want to avoid links from "People also ask" block:

//div[@class="g"]//h3/a[not(ancestor::div[@class="_OKe"])]

Upvotes: 4

Related Questions