使用 Selenium 和 pywinauto 自动上传文件
Automating file upload using Selenium and pywinauto
我正在尝试自动上传表单中的文件。该表格的工作原理如下:
- 一些数据插入
- 点击添加附件按钮
- windows 对话 window 出现
- select 文件
- 打开它
我正在使用 python、Selenium webdriver 和 pywinauto 模块。
描述了类似的方法here,但它只处理文件名而不处理文件路径。
无法使用 Selenium 将键发送到元素,因为没有包含路径的文本框。我尝试使用 AutoIT 和以下代码:
$hWnd = WinWaitActive("[REGEXPTITLE:Otev.*|Op.*)]")
If WinGetTitle($hWnd) = "Open" then
Send(".\Gandalf")
Send("{ENTER}")
Else
Send(".\Gandalf")
Send("{ENTER}")
EndIf
代码基本上是在等待带有标题 Open 或 Otevrit(在 CZ 中)的 window 出现,然后施展魔法。此代码在适当的时候被编译成 .exe 和 运行。
代码工作正常并可以上传,但我无法更改文件路径。如果我想在任何计算机上 运行 我的代码,这是必要的。代码的移动性是必要的,因为它是用于 运行ning Selenium 测试的桌面应用程序的一部分。
我要处理的 window 是:
基本上我想输入我的路径字符串并打开文件位置。之后我会输入文件名并打开它(执行上传)。目前我的代码如下:
# click on upload file button:
WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH, "//*[@class=\"qq-upload- button-selector qq-upload-button\"]"))).click()
# wait two seconds for dialog to appear:
time.sleep(2)
# start the upload
dialogWindow = pywinauto.application.Application()
windowHandle = pywinauto.findwindows.find_windows(title=u'Open', class_name='#32770')[0]
window = dialogWindow.window_(handle=windowHandle)
# this is the element that I would like to access (not sure)
toolbar = window.Children()[41]
# send keys:
toolbar.TypeKeys("path/to/the/folder/")
# insert file name:
window.Edit.SetText("Gandalf.jpg")
# click on open:
window["Open"].Click()
我不确定我的问题出在哪里。输入文件名没问题,我可以用:
window.Edit.SetText("Gandalf.jpg")
但出于某种原因,我无法对路径元素执行相同的操作。 我试过将焦点放在它上面并单击,但代码失败。
感谢您的帮助。
按钮HTML:
<div class="qq-upload-button-selector qq-upload-button" style="position: relative; overflow: hidden; direction: ltr;">
<div>UPLOAD FILE</div>
<input qq-button-id="8032e5d2-0f73-4b7b-b64a-e125fd2a9aaf" type="file" name="qqfile" style="position: absolute; right: 0px; top: 0px; font-family: Arial; font-size: 118px; margin: 0px; padding: 0px; cursor: pointer; opacity: 0; height: 100%;"></div>
试试下面的代码,如有任何问题请告诉我:
WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//input[@type="file"][@name="qqfile"]'))).send_keys("/path/to/Gandalf.jpg")
P.S。您应该将字符串 "/path/to/Gandalf.jpg"
替换为文件的实际路径
我正在尝试自动上传表单中的文件。该表格的工作原理如下: - 一些数据插入 - 点击添加附件按钮 - windows 对话 window 出现 - select 文件 - 打开它
我正在使用 python、Selenium webdriver 和 pywinauto 模块。
描述了类似的方法here,但它只处理文件名而不处理文件路径。
无法使用 Selenium 将键发送到元素,因为没有包含路径的文本框。我尝试使用 AutoIT 和以下代码:
$hWnd = WinWaitActive("[REGEXPTITLE:Otev.*|Op.*)]")
If WinGetTitle($hWnd) = "Open" then
Send(".\Gandalf")
Send("{ENTER}")
Else
Send(".\Gandalf")
Send("{ENTER}")
EndIf
代码基本上是在等待带有标题 Open 或 Otevrit(在 CZ 中)的 window 出现,然后施展魔法。此代码在适当的时候被编译成 .exe 和 运行。
代码工作正常并可以上传,但我无法更改文件路径。如果我想在任何计算机上 运行 我的代码,这是必要的。代码的移动性是必要的,因为它是用于 运行ning Selenium 测试的桌面应用程序的一部分。
我要处理的 window 是:
基本上我想输入我的路径字符串并打开文件位置。之后我会输入文件名并打开它(执行上传)。目前我的代码如下:
# click on upload file button:
WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH, "//*[@class=\"qq-upload- button-selector qq-upload-button\"]"))).click()
# wait two seconds for dialog to appear:
time.sleep(2)
# start the upload
dialogWindow = pywinauto.application.Application()
windowHandle = pywinauto.findwindows.find_windows(title=u'Open', class_name='#32770')[0]
window = dialogWindow.window_(handle=windowHandle)
# this is the element that I would like to access (not sure)
toolbar = window.Children()[41]
# send keys:
toolbar.TypeKeys("path/to/the/folder/")
# insert file name:
window.Edit.SetText("Gandalf.jpg")
# click on open:
window["Open"].Click()
我不确定我的问题出在哪里。输入文件名没问题,我可以用:
window.Edit.SetText("Gandalf.jpg")
但出于某种原因,我无法对路径元素执行相同的操作。 我试过将焦点放在它上面并单击,但代码失败。
感谢您的帮助。
按钮HTML:
<div class="qq-upload-button-selector qq-upload-button" style="position: relative; overflow: hidden; direction: ltr;">
<div>UPLOAD FILE</div>
<input qq-button-id="8032e5d2-0f73-4b7b-b64a-e125fd2a9aaf" type="file" name="qqfile" style="position: absolute; right: 0px; top: 0px; font-family: Arial; font-size: 118px; margin: 0px; padding: 0px; cursor: pointer; opacity: 0; height: 100%;"></div>
试试下面的代码,如有任何问题请告诉我:
WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//input[@type="file"][@name="qqfile"]'))).send_keys("/path/to/Gandalf.jpg")
P.S。您应该将字符串 "/path/to/Gandalf.jpg"
替换为文件的实际路径