1

This is the html line for field I am trying to locate:

<input class="textbox" name="txtUserLoginID" maxlength="25" value="" type="text"/>

My XPath is:

WebElement username = driver.findElement(By.xpath(
"html/body/form/table/tbody/tr[2]/td/table/tbody/tr/td[2]/table/tbody/tr[2]"
+ "/td/table/tbody/tr/td/table/tbody/tr/td[6]/table/tbody/tr/td[2]/table/tbody/tr[3]/td/table/tbody/tr[1]/td/table"
+ "/tbody/tr[2]/td[2]/table/tbody/tr/td/table/tbody/tr[1]/td[3]/input"));

but it seems to not be working after that.

I have tried click() and sendKeys() functions, but neither works. I have tried using name and class but nothing works.

Using:

  • Selenium: v3.4
  • Firefox: v53

error:

{Using=xpath, value=//input[@name='txtUserLoginID'][@type='text']}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
8
  • In the console it is showing error on the xpath line. Please assist
    – Yava29
    Aug 14, 2017 at 22:02
  • 2
    please share the html source code. show us what you have tried. and paste the exception text. Aug 14, 2017 at 23:13
  • 1
    Avoid using absolute XPaths and if possible use ID, Name or css as identifyer in stead of XPath..
    – Patrick
    Aug 15, 2017 at 6:29
  • 1
    We need the actual error generated as well as the html under test for the element in question.
    – mutt
    Aug 15, 2017 at 14:18
  • I tired getting the id and class name as well but no luck in that case. Is there any possiblty that we can shorten this xpath(just curious).
    – Yava29
    Aug 15, 2017 at 15:11

3 Answers 3

4

Because your Xpath is an absolute Xpath (that is, you start at the top of the HTML DOM and trace through all the elements), it's very fragile. If any part of the DOM above the element you're looking for is dynamic, your Xpath breaks and the element can't be found. That means, for instance if one of the tables inserts a new row with a message, instead of row[1] your target might be in row[2].

Because of this, you will probably get a better result if you use locators such as:

  1. Id - if your HTML element has an ID, this is the easiest and fastest way to find your element. ID should be unique and unchanging, but some frameworks do generate dynamic IDs
  2. Name - If Id isn't suitable, name can be a useful target, especially if the Name is unique to the page and not dynamic.
  3. Linktext - For any kind of anchor, link text or partial link text is often the best search method.
  4. Partial Linktext - Partial link text can be used where link text is overly long, such as when other elements are embedded in links.
  5. Tag Name - Searching by tag name will give you a collection of elements with the tag name, which you can then filter further by any of the other locators.
  6. Class name - Class name is particularly useful where your page uses dynamic IDs and names, particularly compound class names. If you have something like class="first second third fourth" each of first, second, third, and fourth is a class and can be used in your locator search.
  7. Css - CSS selectors are a little slower than class name selectors, because you're searching for specific style attributes on the element.
  8. xpath (relative Xpath only, to avoid the issues with an absolute Xpath) - A relative Xpath filter can help to locate specific elements. Using absolute Xpaths is bad news because any change to the page structure will cause the locator to fail.
6
  • 2
    although I agree, this isn't actually an answer to the question asked. Should be a comment and not an answer.
    – mutt
    Aug 15, 2017 at 14:17
  • 2
    I'm going to edit this answer as an exercise in what makes a good answer where you change the focus.
    – Kate Paulk
    Aug 15, 2017 at 15:01
  • Yea tried using that classname and css since there is no id for that but nothing works
    – Yava29
    Aug 15, 2017 at 15:13
  • @Yava29 have you tried using driver.findElements(By.TagName, "input") then using inner text, attribute or something similar to locate the correct input element?
    – Kate Paulk
    Aug 15, 2017 at 15:17
  • @kate Paulk<input class="textbox" name="txtUserLoginID" maxlength="25" value="" type="text"/> This is the html line for field called user name where i have to send keys. i have tried using name and class but nothing works
    – Yava29
    Aug 15, 2017 at 15:28
4

Avoid the use of 'Absolute XPath' if possible in your coding to avoid future rework if there is any change in page structure.

If your element is having attributes like class, name & Type then try to locate the element with the use of Relative XPath.

In your case, you can try with the following element locators-

Most suitable & fast element locator is- cssSelector, so first try with this locator type.

By cssSelector:

driver.findElement(By.CssSelector(".textbox");

By Name:

driver.findElement(By.name("txtUserLoginID"));

By className:

driver.findElement(By.className("textbox"));

By XPath:

//input[@class='textbox']

OR

//input[@name='txtUserLoginID']

OR

//input[@name='txtUserLoginID'][@type='text']

OR

//input[@type='text' AND @class='textbox']

OR

//input[contains(@class,'textbox')]

OR

//input[contains(@name,'txtUserLoginID')]

Similar way you can create your own dynamic XPath.

0

An absolute XPath should start with a /, yours isn't. Better would be to use the relative XPath //input[@name='txtUserLoginID'] (if name is unique). Even better would be to use driver.findElement(By.name("txtUserLoginID")).

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.