0

I'm trying to find elements on a web page that is created using ASP.NET. The problem is ASP.NET prior to version 4.0 generates some long names for the controls so to find a simple "Username" input field I need to use its full name:

var elementName = "LoginControl1_uxLoginControl_UserName";
var username = Browser.TryFind(By.Name(elementName));

and it seems to be working, but I'm looking for something simpler. Is it possible to do the same thing using partial element names or partial Ids? e.g.

var username = Browser.TryFind(By.CssSelector("UserName"));

this should return all the elements that have "UserName" in their names/ids (or even the first element).

1
  • If you need a reference, visit the Selectors spec. Commented Feb 21, 2012 at 17:26

2 Answers 2

2

You can do this using xpath,

var username = Browser.TryFind(By.Xpath("//*[contains( @name , 'UserName')] "));

The other option is to used a css selector if possible

Sign up to request clarification or add additional context in comments.

3 Comments

Using XPath definitely is a good choice. @Oliver, what did you mean by "your now limited to only on type of tag" ?
if you look at the XPath you are now selecting all input tag with a name that contains UserName. Before this would work they any HTML element with a name UserName. Sorry wasn't clear
@Oliver: You can easily replace input with *, no?
2

Try using one of the substring matching attribute selectors:

var username = Browser.TryFind(By.CssSelector("[id$='UserName']"));

That matches any element with an ID ending with UserName.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.