使用Python将参数传递给AutoIt程序

2024-04-18 20:13:51 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是每次在Python中执行文件时上载文件的AutoIt代码。我将文件路径替换为$CmdLine1,以便每次都可以传递一个新的文件路径

ControlFocus("Open","","Edit1")
ControlSetText("Open","","Edit1", "$CmdLine[1]")
ControlClick("Open","","Button1")

如何使用Python传递新的文件路径?我用

os.startfile('path to the autoit.exe file')

我读到java中有一种方法可以传递这样的参数,如下所示

Runtime.getRuntime().exec(r"path to autoit.exe file"+""+"file path to be uploaded");

当我试图使用命令行执行此cmd时,它没有传递所需的参数

AutoIt3.exe C:\Users\Downloads\file_upload.exe C:\Users\Downloads\1.png

enter image description here

它在文本字段中键入相同的$CmdLine1参数,如下所示。 enter image description here

是否有其他方法可以在python的for循环中将新文件名作为参数传递,以便它上载多个文件

以下是向谷歌幻灯片网站添加图像的代码

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import os
options = Options()
options.add_argument("start-maximized")
options.add_argument("user-data-dir=C:\\Users\\praba\\AppData\\Local\\Google\\Chrome\\User Data\\")
#options.add_argument("--profile-directory='Profile 1'")
driver = webdriver.Chrome(executable_path=r'chromedriver',options=options)
time.sleep(5)
driver.get("https://docs.google.com/presentation/u/0/create?usp=slides_web")
time.sleep(5)
elem = driver.find_element_by_id('insertImageMenuButton')
time.sleep(5)
elem.click()
time.sleep(5)
el1 = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH,'/html/body/div[35]/div[1]/div/span')))
el1.click()
time.sleep(3)
os.startfile(r"C:\Users\praba\Downloads\file_open.exe")

Tags: 文件pathfromimport路径timedriverselenium
2条回答

经过一番努力,我找到了解决办法。问题在于$CmdLine[1]参数上的双引号

ControlFocus("Open","","Edit1")
ControlSetText("Open","","Edit1", $CmdLine[1])
ControlClick("Open","","Button1")

因此,删除双引号并从命令行调用exe文件将打开所需的文件

os.system('commandline to execute autoit program with the uploading file path')

你能再加一点背景吗?您将如何检索新文件路径?我会根据你提供的信息来回答

如果是以自动方式,理论上,您应该能够使用检索到的新路径(字符串)调用ControlSetText

如果需要手动输入,可以调用类似input('Please enter the new file location. \n')的函数,并将其存储在变量中,然后传递到ControlSetText

相关问题 更多 >