【问题标题】:Prevent file picker dialog from opening - Selenium GeckoDriver阻止打开文件选择器对话框 - Selenium GeckoDriver
【发布时间】:2021-08-31 07:59:11
【问题描述】:

我正在尝试通过发送密钥(文件路径)使用 Selenium 上传文件。它可以工作,但它也会打开文件选择器对话框,并且永远不会关闭。虽然它不会出现任何问题,但在 headless 中它会使测试失败并出现错误:Failed to decode response from marionetteTried to run command without establishing a connection

如何防止文件选择器对话框打开?我试过this solution,但它似乎不起作用。

【问题讨论】:

    标签: javascript selenium unit-testing firefox geckodriver


    【解决方案1】:

    您应该只测试需要测试的内容。

    文件选择器对话框不应该是自动化的。

    请查看以下博客文章,其中他演示了如何在没有文件选择器的情况下上传文件: https://www.guru99.com/upload-download-file-selenium-webdriver.html

    编辑:

    您应该尝试直接使用 JSExecutor 设置<input>value 属性:

    WebElement element = driver.findElement(...);
    JavascriptExecutor executor = (JavascriptExecutor)driver;
    executor.executeScript("arguments[0].value='C:/././hello.png';", element);
    

    【讨论】:

    • 我没有使用文件选择器。我只是在发送密钥并且它正在被打开。
    • 你能提供一个元素的 HTML 代码的 sn-p 吗?
    猜你喜欢
    • 2019-06-21
    • 2017-01-08
    • 1970-01-01
    • 2016-03-14
    • 2012-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-12
    相关资源
    最近更新 更多