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

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

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 acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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