【问题标题】:Test Automation. File upload Java and Selenium测试自动化。文件上传 Java 和 Selenium
【发布时间】:2015-02-25 18:28:13
【问题描述】:

我正在尝试上传文件但没有成功,我找到了这个例子:

WebElement element = getSupport().getDriver().findElement(By.xpath(".//input[@type='file']"));
element.sendKeys("D:/Profiles/user/workspace/copla-selenium/src/test/resources/datasets/default/test-image.jpg");

但我收到此错误:

2015-02-23 17:32:59 ERROR root:97 - 测试失败 org.openqa.selenium.WebDriverException:未知错误:无法聚焦 元素

有什么想法吗?谢谢!

【问题讨论】:

  • 应该有一个input 元素负责上传字段。
  • 我真的不明白你的意思,在这个例子中就像我做的那样:example
  • 查看该线程中使用的 xpath 指向 input 元素 - 你的指向 a 元素。这可能是这里的问题。
  • 我知道你的意思..我改变了它,现在我没有收到任何错误但它没有加载图像
  • 显示你要上传文件的部分代码。

标签: java selenium automated-tests


【解决方案1】:

您是否使用 webdriver ,如果是,则 sendKeys 永远不会适用于 webdriver。 如果您想使用 webdriver,请尝试使用 autoIt 或机器人类进行文件上传。 否则你可以像这样使用远程 webdriver:

RemoteWebDriver rdriver = new RemoteWebDriver(caps);
    rdriver.setFileDetector(new LocalFileDetector());
    WebElement element = rdriver.findElement(By
            .xpath(".//input[@type='file']"));
    element.sendKeys("D:/Profiles/user/workspace/copla-selenium/src/test/resources/datasets/default/test-image.jpg");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-13
    • 1970-01-01
    • 2011-09-01
    • 2013-12-24
    • 2020-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多