简体   繁体   English

如何在 python exe 文件中传递 arguments?

[英]How to pass arguments in python exe files?

I have created.exe for below code.我为以下代码创建了.exe。 I created /dist and now try to execute.exe but nothing has happened.我创建了/dist,现在尝试执行.exe,但什么也没发生。 Please suggest how to proceed or I am doing something wrong?请建议如何进行或我做错了什么? Is it possible to execute below code using.exe.是否可以使用.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 编译python程序后可能重复,如何输入arguments

Answer of ashwinjv from the above link来自上述链接的 ashwinjv 的答案

If you click on the exe to open it: Usually, when you double click the exe, there is only one argument which is <EXEfilename> .如果您单击 exe 打开它: 通常,当您双击 exe 时,只有一个参数是<EXEfilename> Create a shortcut for that exe.为该 exe 创建一个快捷方式。 In the properties for that shortcut, you will see a property called Target which will contain <EXEfilename> change that to <EXEfilename> <arg1> <arg2> .在该快捷方式的属性中,您将看到一个名为 Target 的属性,其中包含<EXEfilename>将其更改为<EXEfilename> <arg1> <arg2> When you use this shortcut to open the exe, it calls the target, which is this call <EXEfilename> <arg1> <arg2> .当您使用此快捷方式打开 exe 时,它会调用目标,即 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>然后您可以使用 sys.argv 访问 arg1 和 arg2 如果您使用命令行:只需将其称为C:\> <EXEfilename> <arg1> <arg2>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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