Python Selenium 通过 Windows 上传上传文件

Car*_*585 4 python selenium file-upload python-2.7

我阅读了很多关于并且每个人都建议不要使用 Windows 上传并直接写入我的文件路径的建议;我什至尝试使用在论坛中找到的一些命令,例如:

swicthTo()
switch_to_window()
window_handles
Run Code Online (Sandbox Code Playgroud)

但我还没有找到任何解决方案。我的主要问题是当时我没有空间直接发送我的文件路径(见下图,介绍路径的空间是灰色的)但我只能选择单击“浏览”并打开 Windows上传者:

在此处输入图片说明

你知道如何切换到上传窗口的Windows并介绍我的文件吗?

我什至以这种方式尝试:

browse=wait(".//*[@id='fileinput']") #open the window upload
browse.click()
time.sleep(1)


def handle_dialog(element_initiating_dialog, dialog_text_input):
    upload_dialog = driver.switch_to_active_element
    print (upload_dialog)
    upload_dialog.send_keys(dialog_text_input)
    upload_dialog.send_keys(selenium.webdriver.common.keys.Keys.ENTER) # the ENTER key closes the upload dialog, other thread exits

handle_dialog(browse, "foobar.txt")
Run Code Online (Sandbox Code Playgroud)

我找到了窗户,当我打印时,我有这个对象:

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")>>
Run Code Online (Sandbox Code Playgroud)

但是当我尝试执行 send_keys 后,我收到此错误:

回溯(最近一次调用):文件“C:\Users\carlo.agresta\Desktop\IE - iQsonar.py”,第 149 行,在 handle_dialog(browse, "foobar.txt") 文件“C:\Users\carlo .agresta\Desktop\IE - iQsonar.py”,第 145 行,在 handle_dialog upload_dialog.send_keys(dialog_text_input) AttributeError: 'function' 对象没有属性 '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")
Run Code Online (Sandbox Code Playgroud)

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

非常感谢提前

m.e*_*ewa 8

使用 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")
Run Code Online (Sandbox Code Playgroud)