Reputation: 55
Scenario:
The xpaths of some links are:
.//*[@id='rso']/div[2]/div/div[2]/div/div/h3/a
.//*[@id='rso']/div[2]/div/div[3]/div/div/h3/a
.//*[@id='rso']/div[2]/div/div[4]/div/div/h3/a
.//*[@id='rso']/div[2]/div/div[5]/div/div/h3/a
.//*[@id='rso']/div[2]/div/div[6]/div/div/h3/a
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
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