![](/img/trans.png)
[英]Is it possible to pass arguments to a python made exe at runtime?
[英]How to pass arguments in python exe files?
我为以下代码创建了.exe。 我创建了/dist,现在尝试执行.exe,但什么也没发生。 请建议如何进行或我做错了什么? 是否可以使用.exe 执行下面的代码。 请建议。
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")
来自上述链接的 ashwinjv 的答案
如果您单击 exe 打开它: 通常,当您双击 exe 时,只有一个参数是<EXEfilename>
。 为该 exe 创建一个快捷方式。 在该快捷方式的属性中,您将看到一个名为 Target 的属性,其中包含<EXEfilename>
将其更改为<EXEfilename> <arg1> <arg2>
。 当您使用此快捷方式打开 exe 时,它会调用目标,即 this call <EXEfilename> <arg1> <arg2>
。 然后您可以使用 sys.argv 访问 arg1 和 arg2 如果您使用命令行:只需将其称为C:\> <EXEfilename> <arg1> <arg2>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.