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 (
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?
CodePudding user response:
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);