【问题标题】:import file from folder using selenium for python使用 selenium for python 从文件夹导入文件
【发布时间】:2018-12-11 17:52:16
【问题描述】:

使用 Selenium for Python 从网页下载文件后,我想将该文件上传到另一个页面。到目前为止,我一直在使用我找到的建议解决方案,但我一直让元素不可见。

代码如下:

element = driver.find_element_by_id('id_in_page')
driver.execute_script("$(arguments[0]).click();", element)
element.send_keys('path_to_folder/file_to_upload')

selenium.common.exceptions.WebDriverException:消息:未知错误:无法聚焦元素。

我试图点击元素,因为我注意到页面有一个 td class="hide" 我认为这可能是导致问题的原因。任何建议将不胜感激!

【问题讨论】:

  • 你能用相关的HTML更新问题吗?可以分享网址吗?

标签: python python-2.7 selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

你可以试试这个:

from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
...
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.ID, 'id_in_page')))
element = driver.find_element_by_id('id_in_page')

actions = ActionChains(driver)
actions.move_to_element(element)
actions.click()
actions.send_keys('path_to_folder/file_to_upload')
actions.perform()
...

【讨论】:

  • 谢谢,但没用。对不起,我忘了提到我已经尝试过使用 ActionChains 但仍然没有。看来我需要从我的机器中选择文件,因为 send_keys 似乎无效
猜你喜欢
  • 2018-08-19
  • 2013-02-19
  • 2020-04-26
  • 1970-01-01
  • 2013-08-26
  • 2018-07-19
  • 2019-06-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多