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/…Syrus– Syrus2018年05月24日 12:43:38 +00:00Commented May 24, 2018 at 12:43
-
Can you share the URL?Bill Hileman– Bill Hileman2018年05月24日 13:56:23 +00:00Commented May 24, 2018 at 13:56
-
1use explicit wait statement and tryVel Guru– Vel Guru2018年05月25日 04:36:36 +00:00Commented May 25, 2018 at 4:36
-
@joe I have tried that way as well but that too isn't working.Satadip Ghosh– Satadip Ghosh2018年05月25日 06:12:03 +00:00Commented 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– Syrus2018年05月25日 08:08:05 +00:00Commented May 25, 2018 at 8:08
4 Answers 4
There may be several reasons.
Page not correctly loaded at the time. To resolve this
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
you can add page load timeout.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)", ""); }
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); } }
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; }
}
}
}
you can try :
selenium.waitForPageToLoad(PAGE_TO_LOAD_TIMEOUT);
-
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– Kate Paulk2021年08月27日 11:34:34 +00:00Commented Aug 27, 2021 at 11:34
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");
Explore related questions
See similar questions with these tags.