7

How to get the maximum allowed character in any text box eg.user name, email address, in selenium webdriver ?

5

You can find the field and get the maxlength attribute

max_length=find('input#id').getAttribute('maxlength')

In the IDE I think it would be something like this to store it in 'x':

<td>storeAttribute</td>
<td>//input[@id='identifier']@maxlength</td>
<td>x</td>

although I would use css for the identifier, more like:

input#identifier@maxlength

Note: in terms of how many characters the user is putting in the field - https://stackoverflow.com/a/3166107/631619 shows over a million allowed so finding out by by using a loop might take a long while (due to the need to actually interact with the browser) depending on whether there is a server side limit being enforced - that will require a POST or PUT so you'll need thousands of them

  • @FDM I think you mean "which is not always present". I just checked this site, slack, my bank and they ALL have the maxlength attribute on input fields :). Google did not however. So not dependable on many sites but if it is used on the site in question it will work. so depends on that. not really 99% 'not' though for major sites unless you have more stats on that – Michael Durrant Jul 15 '17 at 11:34
  • Yeah sorry, not 99 out of 100 but in my experience, with in-house company apps I have never seen it used. – FDM Jul 15 '17 at 15:52
2

Different scenarios:

1) maxlength attribute

In HTML you might have a maxlength attribute on your input tag. In this case you can extract the value with Selenium's GetAttribute("maxlength") or similar.

2) No maxlength attribute

In this case, the validation (if any) is either server-side or client-side (Javascript). Server-side code is impossible to see (ask the developers), client-side Javascript can be analysed but that will be a manual process rather than a Selenium script.

However, if you still want to use Selenium to figure it out, here's the pseudo-code logic on how to do it:

  1. Enter a character N times in the input field (use a loop).
  2. Get the input's value and see if it is N characters long.
    • If it is, start at 1. again with N = N+1.
    • If it's not, the maximum length is N-1

Edit: it's possible that you could enter 500 characters but the server-side validation is at 100, for example. To test this, you'll need to perform another action (like saving) to see if any validation gets triggered.

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.