简体   繁体   中英

How to pass arguments in python exe files?

I have created.exe for below code. I created /dist and now try to execute.exe but nothing has happened. Please suggest how to proceed or I am doing something wrong? Is it possible to execute below code using.exe. Please suggest.

from selenium import webdriver  
from time import sleep  
import sys  

driver = webdriver.Firefox()  # To connect to web browser use webdriver imported from selenium
driver.get('https://web.whatsapp.com/')

name = sys.argv[1]
msg = sys.argv[2]
filepath = sys.argv[3]


def whatsapp(name, msg="", filepath=""):

    input('Enter anything after scanning QR code')

    user = driver.find_element_by_xpath('//span[@title = "{}"]'.format(name))
    user.click()

    if msg != "":
        msg_box = driver.find_element_by_xpath("//*[@id='main']/footer/div[1]/div[2]/div/div[2]")
        msg_box.send_keys(msg)  # To send message

        button = driver.find_element_by_xpath("//*[@id='main']/footer/div[1]/div[3]/button")
        button.click()  # click send button

    if filepath != "":
        attachment_box = driver.find_element_by_xpath('//div[@title = "Attach"]')
        attachment_box.click()  # click attachment icon button

        image_box = driver.find_element_by_xpath(
        '//input[@accept="image/*,video/mp4,video/3gpp,video/quicktime"]')
        image_box.send_keys(filepath)  # To send filepath

        sleep(3)  # use to set delay time of 3 sec. It will first find the image box,

        send_button = driver.find_element_by_xpath('//span[@data-testid="send"]')
        send_button.click()  # click send button


if name != "":
    whatsapp(name, msg, filepath)  # parameters are passed at run time using sys.argv

    if msg != "" and filepath == "":
        print("Only message sent successfully")
    elif msg == "" and filepath != "":
        print("Only attachment sent successfully")
    elif msg == "" and filepath == "":
        print("Both message and attachment not sent")
    else:
        print("Both message and attachment sent successfully")
else:
    print("name or group name cannot be empty")

Possible duplicate of after compiling python program, how to input arguments

Answer of ashwinjv from the above link

If you click on the exe to open it: Usually, when you double click the exe, there is only one argument which is <EXEfilename> . Create a shortcut for that exe. In the properties for that shortcut, you will see a property called Target which will contain <EXEfilename> change that to <EXEfilename> <arg1> <arg2> . When you use this shortcut to open the exe, it calls the target, which is this call <EXEfilename> <arg1> <arg2> . You can then access arg1 and arg2 using sys.argv If you use command line: Just call it as C:\> <EXEfilename> <arg1> <arg2>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM