0

I have a list and I would like to use a random choice from that list.

subjects = ['Beauty', 'Fashion', 'Hair', 'Nails', 'Skincare & Makeup', 'News']
random_item = random.choice(subjects)
print(random_item)

driver.find_element_by_xpath('//select[@name='input_4']/option[@value='random_item']').click()

I am able to print a random choice but (random_item) is not working when used inside the driver.find_element_by_xpath command.

Am I doing something wrong or is there something I should add to it?

programiss
  • 255
  • 1
  • 5
  • 16
  • 2
    You need to interpolate the value into the string, e.g. `"@value={}".format(random_item)`. See https://docs.python.org/2/library/stdtypes.html#str.format – jonrsharpe Jan 19 '15 at 22:01

1 Answers1

1

You cannot have an identifier be adjacent to a string literal; that is invalid syntax.

If you want to insert values into a string literal, you can use str.format:

driver.find_element_by_xpath('//select[@name={}]/option[@value={}]'.format(input_4, random_item)).click()
  • Or `'//select[@name=' + input_4 + ']/option[@value=' + random_item + ']'` if you're averse to (the wonderful and amazing) `format` style. – cod3monk3y Jan 19 '15 at 22:03