1

CODE:

driver.findElement(By.name("USER")).sendKeys("XYZ");
driver.findElement(By.name("PASSWORD")).sendKeys("abc");

I have tried various ways not just name but by xpath, css selectors but every time the error remained same. While inspecting the elements the below is the result. As seen in other answers it is not in iframe. I have also tried putting a wait statement but no luck.

HTML:

<input class="news" name="USER" value="" size="13" style="width:100%;">
<input class="news" name="PASSWORD" value="" size="13" style="width:100%;" 
type="password">

Exception:

org.openqa.selenium.NoSuchElementException: no such element: Unable to 
locate element: {"method":"name","selector":"USER"}
Bharat Mane
6,78512 gold badges42 silver badges69 bronze badges
asked May 24, 2018 at 7:10
6
  • I had the same problem, sometimes it happen if the page is not properly at the time of searching the element. Try this PageFactory.initElements(driver, "class name".class); Refer this page. It will give you a proper idea toolsqa.com/selenium-webdriver/… Commented May 24, 2018 at 12:43
  • Can you share the URL? Commented May 24, 2018 at 13:56
  • 1
    use explicit wait statement and try Commented May 25, 2018 at 4:36
  • @joe I have tried that way as well but that too isn't working. Commented May 25, 2018 at 6:12
  • its better if you have given the code. I had same problem. Have tried using "PageFactory.initElements"? Take this for an example GoogleSearchPage page = PageFactory.initElements(driver, GoogleSearchPage.class); Commented May 25, 2018 at 8:08

4 Answers 4

1

There may be several reasons.

  1. Page not correctly loaded at the time. To resolve this driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS); you can add page load timeout.

  2. Element is not actually visible in screen and you need to scroll down or scroll up to that element

    public void scrollDown(){
     JavascriptExecutor js = (JavascriptExecutor) driver;
     js.executeScript("window.scrollBy(0,250)", "");
    }
    
  3. Another solution is Add time to wait until element visible

    public WebElement waitUntilElementVisible(WebDriver driver, WebElement element, int delay) {
     try{
     WebDriverWait wait = new WebDriverWait(driver, delay);
     return wait.until(ExpectedConditions.visibilityOf(element));
     }catch (NoSuchElementException e){
     throw new RuntimeException("Web element not visible within given time" + element +" Time "+ delay);
     }
    }
    
Bence Kaulics
1,00712 silver badges22 bronze badges
answered Dec 13, 2018 at 5:46
0

In .Net ISearchContext.FindElement() throws NoSuchElementException. Based on the official Selenium documentation I speculate that in Java it does not throw exception or WebDriverWait.Until knows how to handle it.

WebDriverWait.IgnoreExceptionTypes(typeof(NoSuchElementException)); would help but it does not work.

Solved with static helper method:

using OpenQA.Selenium;
namespace Blah
{
 public static class SearchContextHelper
 {
 public static IWebElement TryFindElement(this ISearchContext searchContext, By by)
 {
 try { return searchContext.FindElement(by); }
 catch (NoSuchElementException) { return null; }
 }
 }
}
answered Aug 18, 2019 at 21:03
0

you can try : selenium.waitForPageToLoad(PAGE_TO_LOAD_TIMEOUT);

Kate Paulk
31.5k8 gold badges56 silver badges109 bronze badges
answered Aug 27, 2021 at 10:20
1
  • Since this is a new answer to an old question with several other answers, could you please expand on your answer to explain how it improves on the existing answers? Commented Aug 27, 2021 at 11:34
-1

If page will be not loaded properly then it might be this kind of error will be occur Please try with this:

WebElement username=driver.findelement(By.name("USER"));
Thread.sleep(2000);
username.sendkeys("test");
WebElement username=driver.findelement(By.name("PASSWORD"));
Thread.sleep(2000);
username.sendkeys("test");
Bence Kaulics
1,00712 silver badges22 bronze badges
answered Dec 14, 2018 at 7:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.