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"}
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/…
    – Syrus
    May 24, 2018 at 12:43
  • Can you share the URL? May 24, 2018 at 13:56
  • 1
    use explicit wait statement and try
    – Vel Guru
    May 25, 2018 at 4:36
  • @joe I have tried that way as well but that too isn't working. 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);
    – Syrus
    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);
        }
    }
    
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; }
        }
    }
}
0

you can try : selenium.waitForPageToLoad(PAGE_TO_LOAD_TIMEOUT);

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?
    – Kate Paulk
    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");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.