我如何在Python Selenium中上传文件?

问题描述 投票:-2回答:1

我在远程PC中启动了一个浏览器,我想将一个zip文件从该远程PC中的本地文件夹导入到我的Web应用程序中。当我单击“导入”按钮时,它会将我重定向到“文件上载”,该文件上载不会显示该帧的任何xpath。我试图获取导入按钮的xpath并使用该对象的send_keys到该对象,如下所示,

importbutton=driver.find_element_by_xpath(".//*@id='templateUploadButton_label']/img")
importbutton.send_keys("C:/../pathtofile")

但它给了我这样的错误

selenium.common.exceptions.WebDriverException:消息:未知错误:无法聚焦元素

但是,当我执行importbutton.click()时,它会打开文件上传框架。

手动步骤是:单击导入 - >打开文件上载框架,然后选择本地路径并单击打开,如果我们可以继续上载并且我点击是,则会显示一个对话框。

python file selenium xpath upload
1个回答
0
投票
.//*@id='templateUploadButton_label']/img

您正在尝试将密钥发送到img元素,而不是input - 因此错误。

更改定位器以使用input定位type="file"元素。

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