【问题标题】:Upload file with Selenium Webdriver Python使用 Selenium Webdriver Python 上传文件
【发布时间】:2017-10-02 05:05:43
【问题描述】:

我已经尝试过这个页面的方法: 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)

【问题讨论】:

  • 请说明带有id="fileUploadProxy"的元素在HTML中是如何表示的。
  • @alecxe
    选择文件

标签: python python-2.7 selenium


【解决方案1】:

问题是 - 您将密钥发送到不可“交互”的 div 元素,不接受密钥 - 因此出现“无法聚焦元素”错误。

您所链接的解决方案背后的想法是使用负责文件上传的type="file" 将密钥发送到input 元素。在您的 HTML 中找到此元素并向其发送密钥。

请注意,此元素可能是不可见的。在这种情况下,您应该首先make it visiblesend_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 的上下文,然后才执行元素搜索。

【讨论】:

  • 我想我找到了 &lt;input type="file" name="fileToUpload" id="fileToUpload2" class="fileToUpload"&gt; 所以我将代码更改为 > file_button = browser.find_element_by_id('fileToUpload2') 但它仍然抛出错误?也尝试添加 .click() 。
  • @Phil 现在抛出哪个错误?元素是否可见/可编辑?
  • 当前错误 A 部分 > File "test.py", line 88, in upload_students_results file_button = browser.find_element_by_id('fileToUpload2') File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 282, in find_element_by_id return self.find_element(by=By.ID, value=id_) File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 784, in find_element 'value': value})['value']
  • B部分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.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"fileToUpload2"}
  • 谢谢,它对我有用。它也在 iframe 中。所以我切换到driver.switch_to.frame(browser.find_element_by_name('frame_element'))
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2023-03-06
    • 2016-10-19
    • 1970-01-01
    • 1970-01-01
    • 2020-09-13
    • 2013-09-20
    • 1970-01-01
    • 2015-03-22
    • 2015-12-23
    相关资源
    最近更新 更多