1

I have this XPath below and I want that one the moment that values are inserted to be random and can be a max of 6 letter + numbers! Any help will be appreciate!

driver.FindElement(By.XPath("//*[@id=\"sec_info\"]/table/tbody/tr[4]/td[2]/input[2]")).SendKeys("");
  • 4
    Keep in mind that locating an element and sending interactions to an element are separate actions. If you're wanting to know how to send a random string to an element it doesn't matter how you've located the the element. – sphennings Feb 22 at 15:16
3

The SendKeys command can take an arbitrary string passed as an argument. All you need to do is generate the random string you want to be sent to the webelement using the best practices of the language you are developing in.

For instance in Python you'd generate a 6 character alphanumeric string with the following.

randstring = ''.join(random.choices(string.ascii_uppercase + string.digits, k=6))
element.send_keys(randstring)

You can read more about generating random strings in Python here

There are similar posts about generating random strings in Java, C#, and Javascript.

1

I was able to find a method to do the job for me, but thank you for every answer

private static Random random = new Random();
        private static string RandomKey(int length)
        {
            const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            return new string(Enumerable.Repeat(chars, length)
            .Select(s => s[random.Next(s.Length)]).ToArray());
        }
  • 1
    Nice to see you circling back and sharing what you found. Oftentimes, just knowing what is the right question to ask is all you need to be able to go and find the answer. Note how simplifying your question from "How to insert random numbers or letter inside a text area using selenium and SendKeys()?" to "How do I generate a random string of text?" turned this into something you were able to search for. – sphennings Feb 25 at 15:32
0

I am assuming from your one line that you may be working in NodeJS. A popular package for producing random values for testing is chance. The string method may be useful to you.

.SendKeys(Chance.string({length: 5});

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.