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".
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?