-1

So i want to automatically buy a shoe from nike but i get thies error:

usage: main.py [-h] [--login_time LOGIN_TIME] [--release_time RELEASE_TIME] [--screenshot_path SCREENSHOT_PATH] [--html_path HTML_PATH] [--page_load_timeout PAGE_LOAD_TIMEOUT] [--driver_type {firefox,chrome}] [--headless] [--select_payment] [--purchase]
               [--num_retries NUM_RETRIES]
               usernME PASSOWRD https://www.nike.com/de/t/air-jordan-1-low-herrenschuh-PPffMw/553558-136 EU 44
main.py: error: the following arguments are required: username, password, https://www.nike.com/de/t/air-jordan-1-low-herrenschuh-PPffMw/553558-136, EU 44

Here is the code:

def main():
parser = argparse.ArgumentParser(description='Processing input values for run')
parser.add_argument("username")
parser.add_argument("password")
parser.add_argument("https://www.nike.com/de/t/air-jordan-1-low-herrenschuh-PPffMw/553558-136")
parser.add_argument("EU 44")
parser.add_argument("--login_time", default=None)
parser.add_argument("--release_time", default=None)
parser.add_argument("--screenshot_path", default=None)
parser.add_argument("--html_path", default=None)
parser.add_argument("--page_load_timeout", type=int, default=2)
parser.add_argument("--driver_type", default="chrome", choices=("firefox", "chrome"))
parser.add_argument("--headless", action="store_true")
parser.add_argument("--select_payment", action="store_true")
parser.add_argument("--purchase", action="store_true")
parser.add_argument("--num_retries", type=int, default=1)
args = parser.parse_args()
driver = None

if args.driver_type == "chrome":
    options = webdriver.ChromeOptions()
    if args.headless:
        options.add_argument("headless")
    if sys.platform == "win32":
        executable_path = "./bin/win_chromedriver.exe"
    else:
        raise Exception("Unsupported operating system. Please add your own Selenium driver for it.")
    driver = webdriver.Chrome(executable_path=executable_path, options=options)
else:
    raise Exception("Unsupported browser. Please use chrome for now")

run(driver=driver, username=args.username, password=args.password, url=args.url, shoe_size=args.shoe_size,
    login_time=args.login_time, release_time=args.release_time, page_load_timeout=args.page_load_timeout,
    screenshot_path=args.screenshot_path, html_path=args.html_path, select_payment=args.select_payment,
    purchase=args.purchase, num_retries=args.num_retries)

Before this error the code was with required=True but then there was an error so i removed it and i get this error

Noam
  • 1
  • 2
  • 3
    You misunderstand how `argparse` is used. You would not add argument entries for the values you are supplying (like the URL and whatever "EU 44" is). You pass that to the `run` function. `parser.add_argument` is used to add things that must be specified on the command line. – Tim Roberts Sep 13 '22 at 17:22
  • @TimRoberts so how do i have to change the run function – Noam Sep 13 '22 at 17:30
  • 1
    heads-up you may have inadvertently provided your username and password in your Question! – ti7 Sep 13 '22 at 17:32
  • 1
    @Noam Please change your password, you've leaked it onto the internet. (Even if you've edited it out of the question doesn't mean it's not out there.) – AKX Sep 13 '22 at 17:39
  • @Noam "How do I have to change"... is not a good question, since we don't know what `run()` is, or what changes you've done that have broken things. – AKX Sep 13 '22 at 17:40

1 Answers1

2

Here is basically what you want to do. I haven't tested this, but it's approximately correct:

def main():
    parser = argparse.ArgumentParser(description='Processing input values for run')
    parser.add_argument("username")
    parser.add_argument("password")
    parser.add_argument("url")
    parser.add_argument("shoe_size")
    parser.add_argument("--login_time", default=None)
    parser.add_argument("--release_time", default=None)
    parser.add_argument("--screenshot_path", default=None)
    parser.add_argument("--html_path", default=None)
    parser.add_argument("--page_load_timeout", type=int, default=2)
    parser.add_argument("--driver_type", default="chrome", choices=("firefox", "chrome"))
    parser.add_argument("--headless", action="store_true")
    parser.add_argument("--select_payment", action="store_true")
    parser.add_argument("--purchase", action="store_true")
    parser.add_argument("--num_retries", type=int, default=1)
    args = parser.parse_args()
    driver = None

    if args.driver_type == "chrome":
        options = webdriver.ChromeOptions()
        if args.headless:
            options.add_argument("headless")
        if sys.platform == "win32":
            executable_path = "./bin/win_chromedriver.exe"
        else:
            raise Exception("Unsupported operating system. Please add your own Selenium driver for it.")
        driver = webdriver.Chrome(executable_path=executable_path, options=options)
    else:
        raise Exception("Unsupported browser. Please use chrome for now")

    run(driver=driver, username=args.username, password=args.password, url=args.url, shoe_size=args.shoe_size,
        login_time=args.login_time, release_time=args.release_time, page_load_timeout=args.page_load_timeout,
        screenshot_path=args.screenshot_path, html_path=args.html_path, select_payment=args.select_payment,
        purchase=args.purchase, num_retries=args.num_retries)

if __name__=="__main__":
    main()

You don't embed any of the details in the script. It's just a helper.

Then in your shell:

python main.py <username> <password> "https://www.nike.com/de/t/air-jordan-1-low-herrenschuh-PPffMw/553558-136" "EU 44" --purchase
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • If you (Noam) are running your python script through something like Pycharm or IDLE and are unsure how to pass arguments to a script, see https://stackoverflow.com/questions/33102272/pycharm-and-sys-argv-arguments, https://stackoverflow.com/questions/2148994/when-running-a-python-script-in-idle-is-there-a-way-to-pass-in-command-line-arg, or take the plunge and learn how to run your scripts through the shell with something like https://stackoverflow.com/questions/4621255/how-do-i-run-a-python-program-in-the-command-prompt-in-windows-7. – Kaia Sep 13 '22 at 18:17