You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python上传文件至编码转换网站失败,报ElementNotInteractableException

问题:文件上传至编码转换网站时触发ElementNotInteractableException

我需要批量将本地文件上传到编码转换网站,转换为SHIFT_JIS编码,但执行代码时在input_file.send_keys(path)行报错,提示元素无法交互。

原代码

driver = webdriver.Edge()
driver.get('https://www.freeformatter.com/convert-file-encoding.html')
for path in x_paths[8745:]:


    input_file = WebDriverWait(driver, 10).until(EC.presence_of_element_located((by.CLASS_NAME, 'form-control')))
    input_file = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((by.CLASS_NAME, 'input_file')))
    input_file.send_keys(path)
    target_encoding = Select(driver.find_element(by.NAME, 'targetEncoding'))
    target_encoding.select_by_value('SHIFT_JIS')

    download_btn = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((by.ID, 'convertBtn')))
    download_btn.click()

    sleep(3)

错误信息

ElementNotInteractableException: Message: element not interactable
  (Session info: MicrosoftEdge=119.0.2151.72)
Stacktrace:
    GetHandleVerifier [0x00007FF75A572602+60402]
    Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF75A4F7352+253666]
    (No symbol) [0x00007FF75A2C9AE3]
    (No symbol) [0x00007FF75A313E46]
    (No symbol) [0x00007FF75A311CD3]
    (No symbol) [0x00007FF75A33BA8A]
    (No symbol) [0x00007FF75A30D766]
    (No symbol) [0x00007FF75A30D60D]
    (No symbol) [0x00007FF75A33BDB0]
    (No symbol) [0x00007FF75A30D766]
    (No symbol) [0x00007FF75A355111]
    (No symbol) [0x00007FF75A33B863]
    (No symbol) [0x00007FF75A30C585]
    (No symbol) [0x00007FF75A30B993]
    (No symbol) [0x00007FF75A30CD14]
    Microsoft::Applications::Events::EventProperty::to_string [0x00007FF75A7534A4+1161924]
    (No symbol) [0x00007FF75A38E5F6]
    Microsoft::Applications::Events::EventProperty::~EventProperty [0x00007FF75A44B083+37459]
    Microsoft::Applications::Events::EventProperty::~EventProperty [0x00007FF75A442C4D+3613]
    Microsoft::Applications::Events::EventProperty::to_string [0x00007FF75A7521E4+1157124]
    Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF75A5016A8+295480]
    Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF75A4FCB74+276228]
    Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF75A4FCCA2+276530]
    Microsoft::Applications::Events::ILogConfiguration::operator* [0x00007FF75A4EFA91+222753]
    BaseThreadInitThunk [0x00007FFE4DF4257D+29]
    RtlUserThreadStart [0x00007FFE4F84AA58+40]

解决方法

  • 定位正确的文件上传元素:网站视觉上的上传按钮通常是装饰用的,真正接收文件的是隐藏的<input type="file">元素,直接定位该元素:
    input_file = WebDriverWait(driver, 20).until(EC.presence_of_element_located((by.XPATH, '//input[@type="file"]')))
    
  • 移除冗余定位代码:删除第一行input_file = WebDriverWait(driver, 10).until(EC.presence_of_element_located((by.CLASS_NAME, 'form-control'))),避免变量被无效覆盖。
  • 重置页面状态:每次转换完成后刷新页面,清除残留的文件信息,确保下一次上传环境干净:
    driver.refresh()
    

修改后的完整代码:

driver = webdriver.Edge()
driver.get('https://www.freeformatter.com/convert-file-encoding.html')
for path in x_paths[8745:]:
    # 定位真正的文件上传input
    input_file = WebDriverWait(driver, 20).until(EC.presence_of_element_located((by.XPATH, '//input[@type="file"]')))
    input_file.send_keys(path)
    
    # 选择目标编码
    target_encoding = Select(driver.find_element(by.NAME, 'targetEncoding'))
    target_encoding.select_by_value('SHIFT_JIS')
    
    # 点击转换按钮
    download_btn = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((by.ID, 'convertBtn')))
    download_btn.click()
    
    # 等待下载完成后刷新页面
    sleep(5)
    driver.refresh()

内容的提问来源于stack exchange,提问作者Ten

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 10:12:17