1

i tried using xpath, Id to find elements. even though im getting the error "unable to locate the element" please can anyone help?

HTML Snippet

<input type="text" value="" maxlength="256" id="FileLeafRef_8553196d-ec8d-4564-9861-3dbe931050c8_$onetidIOFile" title="Name Required Field" class="ms-long" size="35">

C# Code

driver.FindElement(By.Id("FileLeafRef_8553196d-ec8d-4564-9861-3dbe931050c8_$onetidIOFile")).SendKeys("Newprojecttest");
7
  • yes can you share the relevant HTML?
    – Paras
    Commented Jul 21, 2016 at 10:45
  • <input type="text" value="" maxlength="256" id="FileLeafRef_8553196d-ec8d-4564-9861-3dbe931050c8_$onetidIOFile" title="Name Required Field" class="ms-long" size="35"> Commented Jul 21, 2016 at 10:50
  • im unable to enter the text in textbox field..... Commented Jul 21, 2016 at 10:55
  • i tried to enter the text in the textbox field, but im getting error "unable to locate element" even after its clicking in text box. Commented Jul 21, 2016 at 11:16
  • can you share your code, how would I suppose to help you, if you don't share the code.
    – Paras
    Commented Jul 21, 2016 at 11:17

5 Answers 5

1

You can use any of the below mentioned way to select that particular element.

driver.FindElement(By.Xpath("//input[@title='Name Required Field']")).sendKeys("Newprojecttest");

OR

driver.FindElement(By.Xpath("//input[starts-with(@Id,'FileLeafRef')]")).sendKeys("Newprojecttest");

Hope it helps!

1
  • I didn't see this answer before posting mine, which has nearly the same content. Voting for this answer. Commented Feb 17, 2017 at 23:56
0

You can try different selectors based on existing value of attributes that do not change or to use a selector based on a fixed part of the attribute.

Here are some css selectors that you can try:

input[id*='FileLeafRef']
input[id*='onetidIOFile']
input[title*='Name']
input[title='Name Required Field']
0

I recommend using a CSS selector, yours would look something like:

var element = driver.FindElement(By.CssSelector("input.ms-long"));
element.SendKeys("Newprojecttest");

Hope this helps

0

This is just a guess based on the HTML you provided, but is the ID different each time the page is loaded? This seems likely based on the fact that the ID contains a GUID.

If that's the case, my recommendation would be to use XPath as follows:

driver.FindElement(By.XPath("//input[contains(@id,'FileLeafRef')]")).SendKeys("Newprojecttest");

This will locate the first element on the page whose ID contains "FileLeafRef", so even if the GUID is unique each time the page is loaded, the target element will be found.

Note that if there are multiple text inputs that have similar IDs, you will have to get more specific.

Alternatively, if the @title attribute is unique among elements on the page, you could use that as well since it is nice and clean:

driver.FindElement(By.XPath("//input[@title='Name Required Field']")).SendKeys("Newprojecttest");
0

in some times you will face this type of problems to find the elements, at that time you can try different methods, mostly it will work for

driver.findelement(By.name("xxxx")).click();

By.name will work most of the times, same i have faced and resolved with these name locator.

2
  • but in the code i commented ..there is no name in that.so i can do that by.name? Commented Jul 21, 2016 at 11:14
  • In Case for element name is mention then you can use for that.like below.<input id="userNameId" class="loginfield" type="text" value="" name="userLogin.userId"/>
    – Rajesh k
    Commented Jul 21, 2016 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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