How to handle the Dynamic Google suggestions using Selenium

Many times we have to handle the suggestions in the search box dynamically. As shown below for the google search page. When typed “selenium” word, it’s showing so many suggestions:-

Now if we want to select only “selenium interview questions” then how to do that?

As these options appears dynamically, so by using fixed locator is not a good approach. We have to use the dynamic approach.

In this above picture, we can find that the entire suggestion box is actually under the “user lists” tags. Under this tags, all the suggestion lists are coming. So xpath of this entire user list block will be “//div[@class=’G43f7e’]” as shown in the below mentioned attachmnet:-

X path checked in the console of the browser using $x(“xpath”)

But to fetch the exact desired element we can use the concept of xpath locator as mentioned below:-

All the lists elements are stored under a list data structure. And then by checking the text value, we can fetch that particular web element inside the for loop. And then we can click on that particular element.