2024-09-27 00:11:36 发布
网友
我试过这个页面上的方法: 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')
但我得到了以下错误:
问题是-您正在向div元素发送密钥,该元素不是“可交互的”,不接受这些键-因此出现“cannot focus element”错误。在
div
您所链接的解决方案的思想是将密钥发送到负责文件上载的type="file"的input元素。在您的HTML中找到这个元素,并向它发送键。在
type="file"
input
请注意,此元素可能是不可见的。在这种情况下,您应该首先make it visible让send_keys()工作。在
send_keys()
更新:
好吧,现在我们至少知道我们想要的元素是什么:
<input type="file" name="fileToUpload" id="fileToUpload2" class="fileToUpload">
由于无法定位此元素,请尝试waiting for it:
或者,检查这个元素是否在iframe中-如果是,您需要切换到iframe的上下文中,然后才执行元素搜索。在
iframe
当我将文件路径作为字符串插入时,也遇到了同样的问题。这是功能性的:file_input.send_keys(os.path.abspath("path/to/the/file.xyz"))
file_input.send_keys(os.path.abspath("path/to/the/file.xyz"))
问题是-您正在向
div
元素发送密钥,该元素不是“可交互的”,不接受这些键-因此出现“cannot focus element”错误。在您所链接的解决方案的思想是将密钥发送到负责文件上载的
type="file"
的input
元素。在您的HTML中找到这个元素,并向它发送键。在请注意,此元素可能是不可见的。在这种情况下,您应该首先make it visible让
send_keys()
工作。在更新:
好吧,现在我们至少知道我们想要的元素是什么:
由于无法定位此元素,请尝试waiting for it:
^{pr2}$或者,检查这个元素是否在
iframe
中-如果是,您需要切换到iframe
的上下文中,然后才执行元素搜索。在当我将文件路径作为字符串插入时,也遇到了同样的问题。这是功能性的:
file_input.send_keys(os.path.abspath("path/to/the/file.xyz"))
相关问题 更多 >
编程相关推荐