Unable to locate an Element By ID in Selenium

109 views
Skip to first unread message

Miss Cherry

unread,
Mar 6, 2017, 3:05:46 AM3/6/17
to webdriver
Hello guys

I am learning automation testing and my code doesn't working in this line.

     driver.findElement(By.id("addButton")).click();
   // driver.findElement(By.id("cart confirm image")).click();

I tried change the search by "Xpath"  but doesn't working too, someone could help me.

org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"login-form-username"}

Bill Ross

unread,
Mar 6, 2017, 3:14:47 AM3/6/17
to webd...@googlegroups.com

Are you sure the failure is coming from the line you give? The error seems to suggest the id being searched for is "login-form-username"

Bill
--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+...@googlegroups.com.
To post to this group, send email to webd...@googlegroups.com.
Visit this group at https://groups.google.com/group/webdriver.
For more options, visit https://groups.google.com/d/optout.

darrell grainger

unread,
Mar 6, 2017, 8:20:55 AM3/6/17
to webdriver
You can test out locators in Chrome using the developer tools. Open the Inspect window (you can right click any where on the window and select Inspect from the context menu). Go to the console. If you want to test a CSS selector you can use:

document.querySelectorAll("selector goes here")

Just change the "selector goes here" to the CSS selector you are testing. For example, in CSS the selector for an id would be: "#addButton". If this returns an empty array then it is not the correct CSS selector.

That said, the error message indicates that it is failing to find the element with id="login-form-username". So you might want to attempt to see if you can locate that. The CSS selector for id is prepend it with #. The XPath locator for id is a little more complicated. Something like id="login-form-username" would be "//*[@id='login-form-username"). To test an XPath locator in the console use:

$x("//*[@id='login-form-username']")

Also note that the top bar of the console allows you to select which frame you are looking at. The 'top' frame is the default or main frame. If there is no <iframe> or <frame> tags then the 'top' frame is the only frame. However, if your login form is in a different frame, you will need to switch to the frame (in Selenium code) before you attempt to find the elements in that form.

A third possibly is that the login form isn't visible. Selenium will not click on elements which are not visible. If you can see it in the DOM but it has something like style='display: none' then it isn't clickable. Maybe you have to click on a login button or something before the form will be visible.

RAJESH MAKINENI

unread,
Mar 8, 2017, 6:45:11 AM3/8/17
to webdriver
The click is not happening because of the time sync use thread.sleep(4000); before the click. 

Chetan Naik

unread,
Mar 9, 2017, 7:15:11 AM3/9/17
to webdriver
Try using the following line if u think the test case fails cos of time limit. 

driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);


Coming to the xPath, try to find another attribute in the html elements. 

HAve u tried using Firebug in FF to find the xPath ?

sandee...@highq.com

unread,
Apr 12, 2017, 2:39:55 AM4/12/17
to webdriver

Hi Miss Cherry,

have your issue resolved ?

Thank you.
Sandeep
Reply all
Reply to author
Forward
0 new messages