adamjmagyar
adamjmagyar

Reputation: 101

Passing variables through Selenium send.keys instead of strings

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.

Upvotes: 7

Views: 12918

Answers (6)

DevAlgot
DevAlgot

Reputation: 1

I got it! Just use:

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

Upvotes: 0

George Spyrou
George Spyrou

Reputation: 61

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??

Upvotes: 0

Clint Hart
Clint Hart

Reputation: 101

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.

Upvotes: 1

meetar
meetar

Reputation: 7611

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)

Upvotes: 3

BenH
BenH

Reputation: 167

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))

Upvotes: 0

Greg
Greg

Reputation: 5588

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)

Upvotes: 0

Related Questions