1

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:

  • change it to "By.id("q")"
  • change the value to "input"
  • change it to "By.className("gLFyf gsfi")

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

  • wait for the element to be visible with a WebDriverWait and a ExpectedCondition
  • look up, if the element in question is inside a frame or iframe (which I couldnt find in the given example of google)

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?

invzbl3
  • 5,872
  • 9
  • 36
  • 76
mousekip
  • 117
  • 10
  • I tried the same code and it's working perfectly fine for me. Try running in chrome driver and see what is happening – Nandan A Oct 02 '21 at 13:15
  • @Nandan A that is odd. Why wouldn´t it work on another pc? Anyways I just tried ChromeDriver and it gives my another error message. This time it is "Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property;". Despite I thought ChromeDriver would only work for... well Chrome. I however want to be able to use it regardless of the browser, hence htmlUnitDriver. – mousekip Oct 02 '21 at 13:23
  • Put a debug point and try.. – Nandan A Oct 02 '21 at 13:25
  • driver = new HtmlUnitDriver(); driver.get("https://www.google.com/"); driver.findElement(By.name("q")).sendKeys("Test"); Thread.sleep(1000); System.out.println("Success"); – Nandan A Oct 02 '21 at 13:25
  • @Nandan A Well the code below is not different in any way (I did try it regardless and it throws the same exception). To your other comment, I am afraid I do not know how to use the debug tools. Are you certain it is a problem with my installation or something? I could provide the pom.xml file if that helps. – mousekip Oct 02 '21 at 13:31

1 Answers1

0

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);
invzbl3
  • 5,872
  • 9
  • 36
  • 76
  • Maybe I am mistaken, but as I understand it, you must have Opera installed for it to work, dont you? And I want the application to work regardless of Computer, browser and OS (although the last one is not mandatory), hence the use of htmlUnitDriver. And if the Opera browser should not be required for the Opera Driver to work, please explain what the path should be pointing to. – mousekip Oct 02 '21 at 23:19
  • Yes, you're right. I've tested using `Opera` as installed browser. I've updated the answer using `.` to represent a relative path in directory, but it can be used also with absolute path directly to your `driver` folder, if needed. – invzbl3 Oct 03 '21 at 00:07
  • Alright, nice to know how to do it with Opera that will definetly aid me in future projects. However as I pointed out in this instance it is required to work regardless of which browser is installed. So do you by chance know how to write this using the htmlUnitDriver? – mousekip Oct 03 '21 at 10:50
  • 1
    Ok, then I guess I will have browser requirements. Thank you! Now I‘m only wondering, what the htmlUnitDriver is used for – mousekip Oct 03 '21 at 18:20
  • @mousekip, about `HtmlUnitDriver` you can read [here](https://www.softwaretestinghelp.com/headless-browser-testing/#What_Is_HtmlUnitDriver). – invzbl3 Oct 03 '21 at 20:27
  • Yeah I just did some experimenting and found out, that there are certain elements, which are not present in the source code of the website. HtmlUnitDriver works for every element except for these elements and of course elements included in them. For example, on the website https://flow.polar.com/ , If you click on log-in and then inspect the E-Mail text field, the element is shown normally. You can see the id, name and class of it. However if you now switch to the source code tab, it simply is not there. Furthermore no element below "sign-in-container" is there. Very peculiar. – mousekip Oct 03 '21 at 20:48