karthik27
karthik27

Reputation: 484

Clicking on Google Result Suggestions

Just for learning purpose, I am trying to click on the third element of the Google Results Suggestions

Clicking on Google Suggestions

In the above picture, i want to click on qubool hai. My code gets the result suggestions and clicks the 3rd element.

List<WebElement> resultsuggestion = driver.findElements(By.cssSelector(".gssb_m > tbody:nth-child(1) > tr"));
new Actions(driver).click(resultsuggestion.get(2));

But Selenium doesn't click on it. Kindly let me know if anything wrong in the above code or suggest me alternative solutions

Upvotes: 2

Views: 157

Answers (2)

Alpha
Alpha

Reputation: 14076

Following is another way I found:

WebElement result = driver.findElement(By.cssSelector(".gssb_m tr:nth-of-type(3)"));
result.click();

.gssb_m tr:nth-of-type(3) : Under class='gssb_m' element, it looks for third tr tag.

Upvotes: 0

ddavison
ddavison

Reputation: 29052

Try changing your code to:

WebElement result = driver.findElement(By.cssSelector(".gssb_m > tbody > tr:nth-child(3)"));
result.click();

using the :nth-child typically is necessary for specifically identifying children. You seem to be trying to find multiples of only 1 tbody.

furthermore, using the Actions class for a simple click is very unnecessary when you have the WebElement#click method.

Upvotes: 1

Related Questions