我已尝试过此页面中的方法: 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')
但是我收到以下错误:
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)
答案 0 :(得分:4)
问题是 - 您正在向div
元素发送密钥,该元素不是可互动的&#34;,不接受密钥 - 因此&#34;无法关注元素&#34;错误。
您已关联的解决方案背后的想法是将密钥发送到负责文件上传的input
type="file"
元素。在HTML中找到此元素并将密钥发送给它。
请注意,此元素可能不可见。在这种情况下,您应首先make it visible send_keys()
才能正常工作。
<强>更新强>
好的,现在我们至少知道哪个元素是我们想要的元素:
<input type="file" name="fileToUpload" id="fileToUpload2" class="fileToUpload">
由于您无法找到此元素,请尝试waiting for it:
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')
或/和,检查此元素是否在iframe
内 - 如果是,则需要切换到iframe
的上下文,然后才执行元素搜索。
答案 1 :(得分:0)
当我将文件路径作为字符串插入时,我遇到了同样的问题。这是有效的:{"requestId":"73f40519-87c8-4037-a9fc-41be82b3b227","status":{"message":"","code":200,"attributes":{}},"result":{"data":[[20608,28920,32912,106744,123080,135200,139296,143464,143488,143560,151584,155688,155752,159784,188520,254016,282688,286968,311360,323832,348408,4344,835648,8336,1343616,12352]],"meta":{}}}