How to solve org.openqa.selenium.NoSuchElementException

I am trying to run this program:

import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.WebDriver; import org.openqa.selenium.htmlunit.HtmlUnitDriver; public class HtmlDriver { public static void main(String[] args) { // Create a new instance of the html unit driver // Notice that the remainder of the code relies on the interface, // not the implementation. WebDriver driver = new HtmlUnitDriver(); // And now use this to visit Google driver.get("http://www.stumbleupon.com/home/"); // Find the text input element by its name WebElement element = driver.findElement(By.name("q")); // Enter something to search for element.sendKeys("Cheese!"); // Now submit the form. WebDriver will find the form for us from the element element.submit(); // Check the title of the page System.out.println("Page title is: " + driver.getPageSource()); } } 

And I get the following exception:

An exception in the stream "main" org.openqa.selenium.NoSuchElementException: Unable to find an element with the name: q System information: os.name: 'Linux', os.arch: 'i386', os.version: '2.6.27- 7-generic ', java.version:' 1.6.0_12 'Driver information: driver.version: HtmlDriver at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByName (HtmlUnitDriver.java:651) at org.openqa.selenium.By $ 4.findElement (By.java:148) at org.openqa.selenium.htmlunit.HtmlUnitDriver $ 4.call (HtmlUnitDriver.java:1133) at org.openqa.selenium.htmlunit.HtmlUnitDriver $ 4.call (4.jall) at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor (HtmlUnitDriver.java:869) at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement (HtmlUnitDriver.java:1130) at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement (HtmlUnitDriver.javahaps30) at com.webdrivertest.HtmlDriver.main (HtmlDriv er.java:20)

Please help me with this.

+4
source share
5 answers

There is no element on this page with name = "q", hence the NoSuchElementException exception. You took an example from Google and changed the site it went to, but it is still looking for the google search box on the page.

+5
source
 import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.htmlunit.HtmlUnitDriver; public class Example { public static void main(String[] args) { // Create a new instance of the html unit driver // Notice that the remainder of the code relies on the interface, // not the implementation. try { WebDriver driver = new FirefoxDriver(); // And now use this to visit Google driver.get("http://www.google.com"); // Find the text input element by its name WebElement element = driver.findElement(By.name("q")); // Enter something to search for element.sendKeys("Cheese!"); // Now submit the form. WebDriver will find the form for us from the element element.submit(); // Check the title of the page System.out.println("Page title is: " + driver.getTitle()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 

When changing HtmlUnitDriver to FirefoxDriver it works!

+2
source

Using the example from the Selenium website, just like this happens, the test fails with the same NoSuchElementException . It also fails to instantiate a browser emulation, such as BrowserVersion.FIREFOX_3 .

Does HtmlUnitDriver work at all? Is there a need to configure it first?

Update: I am behind a proxy and, unlike drivers that use real browsers, this driver does not know about proxies. It must be manually configured in a test case with a call:

 HtmlUnitDriver.setProxy(host, port); 

I still do not understand how to configure it with a username and password for those proxies that require authentication.

+1
source

Try defining WebDriver to use Firefox explicitly:

 WebDriver driver = new FirefoxDriver(); 
+1
source

We Cannot Use Normal WebElement For Submit

Instead, you can try

 WebElement form = driver.findElement(By.id("formid")); //Id of FORM Tag form.submit(); 
0
source

Source: https://habr.com/ru/post/1334309/


All Articles