-1

I'm really new to python (literally started today, 2 hours ago)...

I know php and i want to know if theres a method to introduce variables in to a .py file...

For example in php you have $_GET variable... I want something like this...

python myprogram.py --num 1234567890

To be executed inside...

search_box = driver.find_element_by_name('txtPhone')
search_box.send_keys('$num')
search_box = driver.find_element_by_name('txtPhoneC')
search_box.send_keys('$num')
link = driver.find_element_by_xpath("//select[@name='ddlProducts']/option[text()='10']").click()

How can i do that? (not english native speaker)

NOTE: I dont know if it matters, but im using selenium with chromedriver to make a "bot"

Alberto B.
  • 31
  • 6

1 Answers1

4

You can use the argparse module to parse command-line arguments:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--num', type=int)
args = parser.parse_args()

Then you can refer to the num parameter in your code with:

search_box.send_keys(args.num)

Please read argparse's documentation for details: https://docs.python.org/3/library/argparse.html

blhsing
  • 91,368
  • 6
  • 71
  • 106
  • and how can i put that --num into this: search_box.send_keys('$num') ...with what i replace $num? – Alberto B. Jul 04 '18 at 18:44
  • You should replace `$num` with `args.num`. I've updated my answer to incorporate the parsed argument in your original code. – blhsing Jul 04 '18 at 18:48