使用Python向AutoIt程序传递参数。

问题描述 投票:0回答:1

这是AutoIt的代码,每次在Python中执行时都会上传一个文件。我把文件路径替换成了$CmdLine。1 以便我每次都能传递一个新的文件路径。

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循环中传递一个新的文件名作为参数,使它能上传多个文件?

下面是添加图片到Google slides网站的代码。

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")
python autoit
1个回答
1
投票

经过一番努力,我找到了解决办法。问题是$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')

0
投票

也许你能再补充一点背景资料吗? 你将如何检索新的文件路径? 我会根据你提供的信息尽量回答。

如果是以自动的方式,理论上你应该可以直接调用那个 ControlSetText 与你检索到的新路径(字符串)。

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

© www.soinside.com 2019 - 2024. All rights reserved.