使用SeleniumWebDriverPython上载文件
我已尝试了此页面中的方法: 代码: 但我得到了以下错误:使用SeleniumWebDriverPython上载文件,python,python-2.7,selenium,Python,Python 2.7,Selenium,我已尝试了此页面中的方法: 代码: 但我得到了以下错误: Traceback (most recent call last): File "test.py", line 110, in <module> upload_students_results('Surname, Name') File "test.py", line 91, in upload_students_results file_button.send_keys('/Users/home/D
Traceback (most recent call last):
File "test.py", line 110, in <module>
upload_students_results('Surname, Name')
File "test.py", line 91, in upload_students_results
file_button.send_keys('/Users/home/Downloads/1-Students-and-Parent-Email.csv')
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py", line 349, in send_keys
'value': keys_to_typing(value)})
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py", line 493, in _execute
return self._parent.execute(command, params)
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 249, in execute
self.error_handler.check_response(response)
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 193, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot focus element
(Session info: chrome=58.0.3029.96)
(Driver info: chromedriver=2.29.461585 (0be2cd95f834e9ee7c46bcc7cf405b483f5ae83b),platform=Mac OS X 10.12.4 x86_64)
回溯(最近一次呼叫最后一次):
文件“test.py”,第110行,在
上传学生成绩('姓,名')
上传学生结果中第91行的文件“test.py”
文件按钮。发送密钥(“/Users/home/Downloads/1-Students-and-Parent-Email.csv”)
文件“/Library/Python/2.7/site packages/selenium/webdriver/remote/webelement.py”,第349行,在send_键中
“值”:键到键入(值)})
文件“/Library/Python/2.7/site packages/selenium/webdriver/remote/webelement.py”,第493行,在
返回self.\u parent.execute(命令,参数)
文件“/Library/Python/2.7/site packages/selenium/webdriver/remote/webdriver.py”,执行中的第249行
self.error\u handler.check\u响应(响应)
文件“/Library/Python/2.7/site packages/selenium/webdriver/remote/errorhandler.py”,第193行,在check_响应中
引发异常类(消息、屏幕、堆栈跟踪)
selenium.common.exceptions.WebDriverException:消息:未知错误:无法聚焦元素
(会话信息:chrome=58.0.3029.96)
(驱动程序信息:chromedriver=2.29.461585(0BE2CD95F834E9EE7C46BC7CF405B483F5AE83B),平台=Mac OS X 10.12.4 x8664)
问题是-您正在向div
元素发送密钥,该元素不“可交互”,不接受密钥-因此出现“无法聚焦元素”错误
您链接的解决方案背后的想法是使用负责文件上传的type=“file”
向input
元素发送键。在HTML中找到此元素并向其发送键
请注意,此元素可能不可见。在这种情况下,您应该首先让发送密钥()
更新:
好的,现在我们至少知道哪个元素是我们想要的:
<input type="file" name="fileToUpload" id="fileToUpload2" class="fileToUpload">
或者/和,检查此元素是否在iframe
中-如果在iframe
中,则需要切换到iframe
的上下文中,然后才执行元素搜索。我在将文件路径作为字符串插入时遇到了相同的问题。这是功能性的:file\u-input.send\u-keys(os.path.abspath(“path/to/the/file.xyz”)
请显示带有id=“fileUploadProxy”
的元素是如何在HTML中表示的。@alecxe-Choose-file我想我找到了
所以我把代码改成了>file\u-button=browser。按id(“fileToUpload2”)查找元素
但它仍在抛出错误?还尝试添加了.click()。@Phil现在抛出了哪个错误?元素是否可见/可编辑?当前错误部分A>文件“test.py”,第88行,在“上传学生结果文件”按钮=浏览器中。按id查找元素(“fileToUpload2”)文件“/Library/Python/2.7/site packages/selenium/webdriver/remote/webdriver.py”,第282行,按id返回self。查找元素(by=by.id,value=id)文件“/Library/Python/2.7/site packages/selenium/webdriver/remote/webdriver.py”,第784行,在执行self.error\u handler.check\u response(response)文件中的find_元素'value':value}][value']
Part BFile”/Library/Python/2.7/site packages/selenium/webdriver/remote/webdriver.py”,第249行”/Library/Python/2.7/site packages/selenium/webdriver/remote/errorhandler.py”,第193行,在check_response raise exception类(message、screen、stacktrace)selenium.common.exceptions.NoSuchElementException:message:没有这样的元素:无法找到元素:{“方法”:“id”,“选择器”:“fileToUpload2”}
谢谢,它对我有用。它也在iframe中。所以我切换到bydriver。切换到.frame(浏览器。按名称查找元素('frame\u element'))
<input type="file" name="fileToUpload" id="fileToUpload2" class="fileToUpload">
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
file_upload = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "fileToUpload2"))
)
file_upload.send_keys('/Users/home/Downloads/1-Students-and-Parent-Email.csv')