使用Selenium Webdriver Python上载文件

2024-09-27 00:11:36 发布

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

我试过这个页面上的方法: Upload file with Selenium in Python

代码:

file_button = browser.find_element_by_id('fileUploadProxy')
file_button.send_keys('/Users/home/Downloads/1-Students-and-Parent-Email.csv')

但我得到了以下错误:

^{pr2}$

Tags: 方法代码inbrowseridbyseleniumwith
2条回答

问题是-您正在向div元素发送密钥,该元素不是“可交互的”,不接受这些键-因此出现“cannot focus element”错误。在

您所链接的解决方案的思想是将密钥发送到负责文件上载的type="file"input元素。在您的HTML中找到这个元素,并向它发送键。在

请注意,此元素可能是不可见的。在这种情况下,您应该首先make it visiblesend_keys()工作。在


更新:

好吧,现在我们至少知道我们想要的元素是什么:

<input type="file" name="fileToUpload" id="fileToUpload2" class="fileToUpload">

由于无法定位此元素,请尝试waiting for it

^{pr2}$

或者,检查这个元素是否在iframe中-如果是,您需要切换到iframe的上下文中,然后才执行元素搜索。在

当我将文件路径作为字符串插入时,也遇到了同样的问题。这是功能性的:file_input.send_keys(os.path.abspath("path/to/the/file.xyz"))

相关问题 更多 >

    热门问题