【问题标题】:Python Selenium Upload a file via Windows UploadPython Selenium 通过 Windows Upload 上传文件
【发布时间】:2018-03-09 02:34:21
【问题描述】:

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

swicthTo()
switch_to_window()
window_handles

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

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

我也尝试过这种方式:

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")

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

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")>>

但是当我尝试做 send_keys 之后,我收到了这个错误:

Traceback(最近一次调用最后一次):文件 “C:\Users\carlo.agresta\Desktop\IE - iQsonar.py”,第 149 行,在 处理对话框(浏览,“foobar.txt”)文件“C:\Users\carlo.agresta\Desktop\IE - iQsonar.py”,第 145 行,在 句柄对话框 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")

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

提前非常感谢

【问题讨论】:

  • 您根本不应该打开对话框。因为它会进入 Selenium 无法控制的本机操作系统窗口。您应该做的是将browse.click() 更改为browse.send_keys("&lt;complete file path"&gt;)
  • 是的,我知道,但我没有其他选项,文本框是灰色的,我没有其他方法可以选择我的文件。到目前为止,我可以打开对话框窗口并介绍我的路径,我很疯狂如何接受它,因为命令 .accept() 不起作用,并且只有 2 个底部“打开”和“取消”。解决我的问题的其他选择?有什么建议吗?
  • 手动当我到达这一点时,按 ENTER 我接受窗口/弹出窗口,所以我尝试让我的脚本按 ENTER 键但它也不起作用,因为它用一个替换我的路径点“。” :S
  • 即使它是灰色的,您是否尝试过使用send_keys
  • 是的,我尝试了,它不起作用我真的无能为力:S

标签: python python-2.7 selenium file-upload


【解决方案1】:

使用自动 示例代码:

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")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-21
    • 2015-09-14
    • 1970-01-01
    • 2015-12-11
    • 1970-01-01
    相关资源
    最近更新 更多