【问题标题】:selenium webdriver upload fileselenium webdriver上传文件
【发布时间】:2013-09-20 07:33:37
【问题描述】:

我是 selenium 新手,我有一个将文件上传到服务器的脚本。

在 ide 版本中它会上传文件,但是当我将测试用例导出为 python 2 /unittest / webdriver 时它不会上传它..

它没有给我任何错误,只是没有上传它......

python脚本是:

driver.find_element_by_id("start-upload-button-single").click()
driver.find_element_by_css_selector("input[type=\"file\"]").clear()
driver.find_element_by_css_selector("input[type=\"file\"]").send_keys("C:\\\\Documents and Settings\\\\pcname\\\\Desktop\\\\ffdlt\\\\test.jpeg")

我搜索了解决方案,但除了将其与 AutoIt 或 AutoHotKey 集成之外,我没有找到任何解决方案......

第一行打开Firefox的文件上传框。

【问题讨论】:

  • 脚本还有更多内容吗?我认为像 driver.find_element_by_css_selector("input[type=\"file\"]").submit() 这样的东西是必要的,但我不知道文件输入框如何与 selenium 一起工作。

标签: python selenium selenium-webdriver selenium-ide


【解决方案1】:

您的代码非常适合我(我使用 Firefox、Chrome 驱动程序对其进行了测试)

我怀疑有一件事是过度的反斜杠(\) 转义。

尝试以下操作:

driver.find_element_by_id("start-upload-button-single").click()
driver.find_element_by_css_selector('input[type="file"]').clear()
driver.find_element_by_css_selector('input[type="file"]').send_keys("C:\\Documents and Settings\\pcname\\Desktop\\ffdlt\\test.jpeg")

driver.find_element_by_id("start-upload-button-single").click()
driver.find_element_by_css_selector('input[type="file"]').clear()
driver.find_element_by_css_selector('input[type="file"]').send_keys(r"C:\Documents and Settings\pcname\Desktop\ffdlt\test.jpeg")

【讨论】:

  • 男士们,我已经测试了每个组合以将文件链接到 .send_keys(文件),它对我不起作用,是否有我不知道的设置或什么?
  • @user2782827,您使用哪个版本的 python/selenium?您使用哪种操作系统/浏览器?
  • 是的,对不起,我应该在帖子中写到,Windows7,python 2.7,firefox 22 或 23,selenium 2.3.0
  • @user2782827,我在使用较低版本的 selenium + firefox 回答 this 时遇到问题。尝试将硒升级到最新版本。我使用 selenium 2.35.0 来测试您的代码。 (ubuntu 12.10 64bit, python 2.7.3)
  • hmmmmmmmmmmmmmmmmm, k
【解决方案2】:

你有没有试过这段代码:

driver.find_element_by_css_selector("input[type=\"file\"]").send_keys("C:\\Documents and Settings\\pcname\\Desktop\\ffdlt\\test.jpeg")

【讨论】:

    【解决方案3】:

    如果我从 IDE 运行以下行,它工作得很好,它会上传文件。

    Command | Target                               | Value
    _____________________________________________________________
    open    | /upload                              |
    click   | id=start-upload-button-single        |
    type    | css=input[type="file"]               | C:\\Documents and Settings\\cristian\\Desktop\\ffdl\\MyWork.avi
    

    但是当我为 Python webdriver 导出它时,它只是不上传它,我已经尝试了一切。

    最后的办法是让它与 AutoHotKey 一起工作,但我希望它工作。

    我所做的是测试我在其他网站上/在其他网站上找到的解决方案,看看问题是否仅在我试图上传的网站上(youtube),解决方案有效(例如:@987654321 @) 它们是有效的,您可以将文件上传到大多数服务器,但它无法正常工作。

    感谢您的帮助。

    【讨论】:

    • 好的,我放弃了 webdriver 并使用 RC 完成了。谢谢。
    【解决方案4】:

    这对我有用:

    # Upload file
    elem = driver.find_element_by_name("File")
    elem.send_keys(r"D:\test\testfile04.txt")
    elem = driver.find_element_by_partial_link_text("Upload File")
    elem.click()
    

    【讨论】:

      【解决方案5】:

      使用Pyautowin

      from pywinauto import Desktop
      driver.find_element_by_id("start-upload-button-single").click()
      app = Desktop()['Open']   # About `Open`, checkout upload Dialog title
      app.wait('visible')
      app.Edit.type_keys(R"C:\Documents and Settings\pcname\Desktop\ffdlt\test.jpeg", with_spaces=True)
      app['Open'].click()  # About `Open`, checkout upload Button name
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-07-20
        • 1970-01-01
        • 2017-10-02
        • 2020-10-24
        • 2015-03-22
        • 2015-12-23
        • 2013-09-23
        • 2013-06-17
        相关资源
        最近更新 更多