Python Selenium通过Windows上传上传文件

2024-05-15 10:16:58 发布

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

我读了很多关于和大家的建议,不要使用Windows上传,直接写我文件的路径;我甚至尝试使用论坛上的一些命令,比如:

swicthTo()
switch_to_window()
window_handles

但我还没找到解决办法。 我的主要问题是,我没有空间直接发送我的文件路径(见下图,介绍路径的空间为灰色),但我只能选择单击“浏览”并打开Windows上载程序:

enter image description here

你知道我如何切换到上传窗口,介绍我的朋友吗?在

我试着用这种方法:

^{pr2}$

我找到了窗口,当我打印时,我得到了这个对象:

We are already in the Location->Details Page <bound method WebDriver.switch_to_active_element of <selenium.webdriver.ie.webdriver.WebDriver (session="3e725bb7-40a7-452a-ad90-9cca1d41296a")>>

但是当我尝试发送密钥时,我收到了这个错误:

Traceback (most recent call last): File "C:\Users\carlo.agresta\Desktop\IE - iQsonar.py", line 149, in handle_dialog(browse, "foobar.txt") File "C:\Users\carlo.agresta\Desktop\IE - iQsonar.py", line 145, in handle_dialog upload_dialog.send_keys(dialog_text_input) AttributeError: 'function' object has no attribute 'send_keys'

我找到了一个部分解决方案,我让我的代码表现得好像窗口上传是一个警报,所以我这样做:

browse=wait(".//*[@id='fileinput']")
browse.click()
time.sleep(1)

upload_dialog = driver.switch_to_alert()
print (upload_dialog)

upload_dialog.send_keys("C:\\Users\\carlo.agresta\\Desktop\\V4LabCredentials.csv")

现在我的问题是我不能接受并关闭窗口:有什么建议吗?在

提前谢谢你


Tags: toin路径sendwindowskeysusers建议
1条回答
网友
1楼 · 发布于 2024-05-15 10:16:58

使用autoit 示例代码:

import autoit
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
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.keys import Keys

ActionChains(driver).move_to_element( driver.find_element_by_xpath("//div[@class='upload_button']")).click().perform()
handle = "[CLASS:#32770; TITLE:Open]"
autoit.win_wait(handle, 60)
autoit.control_set_text(handle, "Edit1", "\\file\\path")
autoit.control_click(handle, "Button1")

相关问题 更多 >