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");
  • yes can you share the relevant HTML? – Paras Jul 21 '16 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"> – santhosh nayak Jul 21 '16 at 10:50
  • im unable to enter the text in textbox field..... – santhosh nayak Jul 21 '16 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. – santhosh nayak Jul 21 '16 at 11:16
  • can you share your code, how would I suppose to help you, if you don't share the code. – Paras Jul 21 '16 at 11:17
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!

| improve this answer | |
  • I didn't see this answer before posting mine, which has nearly the same content. Voting for this answer. – VanderLinden Feb 17 '17 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']
| improve this answer | |
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

| improve this answer | |
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");
| improve this answer | |
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.

| improve this answer | |
  • but in the code i commented ..there is no name in that.so i can do that by.name? – santhosh nayak Jul 21 '16 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 Jul 21 '16 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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