javaseleniumselenium-webdriverselenium-gridhtmlunit-driver

Selenium unable to locate element by id/name


This question has been asked many times, but none of the answers seem to work. I am trying to simply locate the search bar on the google front page (https://google.com). If looking at the inspector, you can clearly see, that the name of the search bar is "q".

google with the inspector open

However, I get the exception:

"Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element with name: q"

when I execute the following code:

package pack;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class Klasse  {

    public static void main(String[] args) {

        WebDriver driver = new HtmlUnitDriver();
        driver.get("https://www.google.com/");

        driver.findElement(By.name("q"));

    }
}

When that did not work, I tried the following things:

Then I searched on the internet and came across multiple fixes, which included:

All of those did nothing for me.

I even copied the code from the solution to this question and it couldnt find it: Selenium webdriver click google search

The only thing I always change, is using a HtmlUnitDriver instead of ChromeDriver or FirefoxDriver etc, since I need it to run on devices with different browsers. Maybe that causes the problem? And if that really is the problem, well then how do I do it browser independent?


Solution

  • I've tested solution using class name locator and OperaDriver for locating the search bar on the google and this code example works for me:

    String path = ".\\operadriver_win64\\operadriver.exe";
    OperaOptions options = new OperaOptions();
    options.setBinary(new File(".\\operadriver_win64\\62.0.3331.72\\opera.exe"));
    System.setProperty("webdriver.opera.driver", path);
    
    OperaDriver driver = new OperaDriver(options);
    driver.manage().window().maximize();
    
    driver.get("https://www.google.com/");
    driver.findElement(By.className("RNNXgb"));
    

    To test, if the element was really found, you can add check using Actions class by specifying your text in the search bar:

    Actions actions = new Actions(driver);
    actions.sendKeys("test data");
    actions.build().perform();
    

    In POM I have dependency:

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.53.1</version>
    </dependency>
    

    Also you can use WebDriverManager to instantiate a browser.


    Since Selenium is a tool for automating browsers, not specifying a browser doesn't make sense, because if you have on PC several browsers, you will have ambiguous situation what exact browser to choose.

    You can use, e.g. an external class that creates the WebDriver instance for you, but you still need to specify explicit configurations there to make it work correctly:

    String path = ".\\operadriver_win64\\operadriver.exe";
    OperaOptions options = new OperaOptions();
    options.setBinary(new File(".\\operadriver_win64\\62.0.3331.72\\opera.exe"));
    System.setProperty("webdriver.opera.driver", path);