Python Forum
[Selenium] Xpath Drop Down Use Variable - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: [Selenium] Xpath Drop Down Use Variable (/thread-5488.html)



[Selenium] Xpath Drop Down Use Variable - digitalmatic7 - Oct-07-2017

I wanted to have a variable (random number between 1 and 12) used for my drop down option value..

So instead of value='05' it would be something like value='month', value='day'

Unfortunately it doesn't work. What are my options?

driver.find_element_by_xpath("//SELECT[@name='month']/option[@value='05']").click()
driver.find_element_by_xpath("//SELECT[@name='day']/option[@value='01']").click()
PS - the numbers must be formatted like 01, 02, 03, 04, 05, 06 etc rather than 1, 2, 3, 4, 5


RE: [Selenium] Xpath Drop Down Use Variable - buran - Oct-07-2017

if I get right you want to get random number to be used in the xpath.
>>> from random import randint
>>> month = randint(1, 12)
>>> my_xpath="//SELECT[@name='month']/option[@value='{:0>2}']"
>>> my_xpath.format(month)
"//SELECT[@name='month']/option[@value='12']"



RE: [Selenium] Xpath Drop Down Use Variable - digitalmatic7 - Oct-07-2017

(Oct-07-2017, 06:34 AM)buran Wrote: if I get right you want to get random number to be used in the xpath.
>>> from random import randint
>>> month = randint(1, 12)
>>> my_xpath="//SELECT[@name='month']/option[@value='{:0>2}']"
>>> my_xpath.format(month)
"//SELECT[@name='month']/option[@value='12']"

Hi Buran, thanks for sharing the solution!

This is the only part that confuses me:

{:0>2}
What does that do?


RE: [Selenium] Xpath Drop Down Use Variable - buran - Oct-07-2017

String formatting, so that it prints 02, instead of 2