1

This is the HTML source of the button:

<input value="Google Search" aria-label="Google Search" name="btnK" type="submit">

This is my code:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
//import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class google {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver","C://chromedriver_win32//chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.google.co.in");
            //driver.findElement(By.xpath("//*[@id='TZA4S']/div[1]/div[1]/input")).sendKeys("Kedarnath review");

            driver.findElement(By.xpath("//*[@id='tsf']/div[2]/div/div[1]/div/div[1]/input")).sendKeys("kedarnath collection");
            //driver.findElement(By.xpath("//@id=(//*[@id='tsf']/div[2]/div[3]/input)")).click();

            //driver.findElement(By.xpath("//@id=(//*[@id='tsf']/div[2]/div[1]/div[3]//center/input[1]")).click();


            /*Issue in below line able to click on Search Button*/
            driver.findElement(By.name("btnK")).click();

5 Answers 5

2
//div[@class='FPdoLc VlcLAe']//input[@type='submit' and @value= 'Google Search']

There are 2 matching nodes available. The only difference is the class name. So construct your xpath with the class name

1

This will work:

Solution 1: //div[@class='FPdoLc VlcLAe']//input[@name='btnK']

Explanation: If you are going to use //*[@name='btnK'] or driver.findElement(By.name("btnK")).click(); then there are 2 matching nodes.

enter image description here

Solution 2: In case you want to go with locator name - then try below solution

(//*[@name='btnK'])[ 2] (write 2 in bracket without spaces)

enter image description here

1
  • 3
    Please edit your answer to explain why your suggestion will work and why the OP's find by name failed. That will make your answer more relevant to other situations where someone is having problems with locators
    – Kate Paulk
    May 31, 2019 at 19:00
0

Use,

driver.findElement(By.name("btnK"))[2].click();

Since the name btnK has two matches, so use this above line.

3
  • the above solution is not working and found below exception Exception in thread "main" java.lang.Error: Unresolved compilation problem: The type of the expression must be an array type but it resolved to WebElement at google.main(google.java:25) Dec 23, 2018 at 9:13
  • It should be findElements I think, and in that case if it is an array that will be returned, using index 2 will return the third button if there is or an ArrayOutOfBound exception will be thrown. Also I would not recommend to find an element using such index as if the page changes, a new button is introduced your test will be broken. Dec 23, 2018 at 11:50
  • for findElements the IDE itself showing “The type of the expression must be an array type but it resolved to List <WebElements>” Dec 24, 2018 at 18:07
0
  1. The "Google Search" button XPath: //div[@class="FPdoLc VlcLAe"]//input[1];
  2. The "I'm Feeling Lucky" button XPath: //div[@class="FPdoLc VlcLAe"]//input[2].
0

The below XPath will enable the scripts to click on "Google Search" button:

driver.findElement(By.xpath("//*[@class='FPdoLc VlcLAe']/center/input[1]")).click();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.