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

1

1 Answer 1

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

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

2 Comments

and how can i put that --num into this: search_box.send_keys('$num') ...with what i replace $num?
You should replace $num with args.num. I've updated my answer to incorporate the parsed argument in your original code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.