0

I am working in Selenium. And I have got an error unable to locate element username. I'm using Java language. I am following page object design pattern.

Here is my code..

Pageobject

 @FindBy(how=How.XPATH,using="//*[@id=\'user_name\']")
    WebElement username;
public void setUername(String usernam){
       WebDriverWait wait = new WebDriverWait(driver, 60);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\'user_name\']")));
    username.sendKeys(usernam);
    }

Steps

AppointmentPage appointmentPageobj= PageFactory.initElements(DriverManager.driver,AppointmentPage.class);

        appointmentPageobj.setUername(usernam);

Test

AppointmentPage ex=log.Valid("9845612345","naveen","[email protected]","Demo","2017-03-04","Dr.biju");

Inspect locator:

<input type="text" id="user_name" value="" name="user_name" class="form-control">

Error:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"name","selector":"username"}
  (Session info: chrome=56.0.2924.87)
  (Driver info: chromedriver=2.25.426924 (649f9b868f6783ec9de71c123212b908bf3b232e),platform=Linux 4.4.0-64-generic x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 193 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:49:13 -0700'
System info: host: 'naveen-Inspiron-3542', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-64-generic', java.version: '1.8.0_121'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.25.426924 (649f9b868f6783ec9de71c123212b908bf3b232e), userDataDir=/tmp/.org.chromium.Chromium.JfNimh}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=56.0.2924.87, platform=LINUX, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 650ae51248719121c19384d62127029c
*** Element info: {Using=name, value=username}

I tried to put the locators ID, NAME, XPATH and CSS too. But still same error is coming what to do?

6
  • is your element in an iFrame?
    – Yu Zhang
    Mar 3, 2017 at 6:05
  • @YuZhang no iFrame
    – user24314
    Mar 3, 2017 at 6:11
  • I believe you should use this @FindBy(how=How.XPATH,using=".//*[@id='user_name']")
    – Alok
    Mar 3, 2017 at 6:30
  • @Alok yes I did @FindBy(how=How.XPATH,using=".//*[@id='user_name']") same error is repeating
    – user24314
    Mar 3, 2017 at 6:32
  • Are you waiting for the element to be present in the DOM before performing any action on it. wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("xpath_details"))
    – Alok
    Mar 3, 2017 at 6:38

1 Answer 1

1

Your element is wrong by standard ways. Change as per below :

//*[@id='user_name']

In your code, change lines as per under :

  1. @FindBy(how=How.XPATH,using="//*[@id='user_name']")

  2. wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='user_name']")));

Xpath can not get element attribute with double quote (") symbol. Use single quote (').


Let come to Null Pointer exception

It might come for below scenario :

driver is not initialize properly. So try to change below line with existing :

WebDriverWait wait = new WebDriverWait(DriverManager.driver, 60);

Use DriverManager.driver when you need driver.

9
  • please review my question
    – user24314
    Mar 3, 2017 at 7:29
  • I have added answer please check
    – Sagar007
    Mar 3, 2017 at 7:35
  • Actually this is my error Unable to locate element: {"method":"name","selector":"username"} I edit my question please check
    – user24314
    Mar 3, 2017 at 7:46
  • I tired by using Thread.sleep(1000) still getting same error
    – user24314
    Mar 3, 2017 at 7:47
  • Have you change element as per answer ?
    – Sagar007
    Mar 3, 2017 at 7:48

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.