7

I'm trying to use Selenium for some app testing, and I need it to plug a variable in when filling a form instead of a hardcoded string. IE:

this works name_element.send_keys("John Doe")
but this doesnt name_element.send_keys(username)

Does anyone know how I can accomplish this? Pretty big Python noob, but used Google extensively to try and find out.

6 Answers 6

3

In at least one case, I found that I couldn't pass a variable to send_keys unless I first passed a regular empty string:

inputElement.send_keys("")
inputElement.send_keys(my_text_variable)

It also works as a list:

inputElement.send_keys("", my_text_variable)

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

Comments

1

Try this.

username = r'John Doe'

name_element.send_keys(username)

I was able to pass the string without casting it just fine in my test.

Comments

0

I dont understand...why not just set username="John Doe" I'm having trouble figuring out what you want to do beyond that

username ="John Doe"

def some_function():
   name_element.send_keys(username)

1 Comment

I can confirm: using the Python libraries to access Selenium, this doesn't seem to work.
0

Have you made certain that your variable is a string?

Greg's answer would ensure that. If you're not completely sure, you could try

name_element.send_keys(str(username))

Comments

0

I think username might be a variable in the python library you are using. Try calling it something else like Username1 and see if it works??

Comments

0

I got it! Just use:

var1 = ''.join(otherVariable)
inputElement.send_keys(var1)

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.