【问题标题】:Upload Local Image With Selenium WebDriver (Java)使用 Selenium WebDriver (Java) 上传本地图像
【发布时间】:2018-12-22 23:14:51
【问题描述】:

我看了很多答案,只找到了一些可以在这里结束的点点滴滴,所以基本上我使用 selenium 来选择页面上的文件输入,然后执行一些 javascript 使其可见,然后发送密钥的文件路径。

所有这些都有效,但是当我提交表单时,图像不会显示在最终产品上,例如当我点击提交并查看我的帖子时,没有图像

这是我的代码:

WebElement imageUpload = driver.findElement(By.xpath(("//*[@id=\"FileInputWrapper\"]/input")));
    Thread.sleep(600);
    js.executeScript("arguments[0].setAttribute('type', 'file');", imageUpload);
    Thread.sleep(600);
    imageUpload.sendKeys(computerHome + "/downloads/testImageFolder/testImage.jpg");
    Thread.sleep(600);

在 Selenium 执行此操作后,它会出现在提交图像按钮上方:

这意味着它收到了我的图像,但由于某种原因它也没有? 因为当我点击帖子上的提交时,图像不可见,没有图像。

非常感谢任何想法。

谢谢。

【问题讨论】:

  • 您能否向我们展示相关的 HTML 块 - 工作示例和不工作示例?

标签: java image selenium input upload


【解决方案1】:

以下方法将帮助您浏览文件并选择它。只需传递 3 个必需参数

  1. 元素类型
  2. 元素定位器
  3. 文件路径/资源

代码如下:

/*-------    FileUploading(): This method browse and the upload the selected file    -------*/
    public static void FileUpload(PropertyType ElementType, string Element, string FilePath)
    {
        if (ElementType == PropertyType.Id)
        {
            PropertyCollection.wdriver.FindElement(By.Id(Element)).Click();
        }
        else if (ElementType == PropertyType.XPath)
        {
            PropertyCollection.wdriver.FindElement(By.XPath(Element)).Click();
        }

        Thread.Sleep(3000);
        SendKeys.SendWait(FilePath);
        Thread.Sleep(3000);
        SendKeys.SendWait(@"{Enter}");
        Thread.Sleep(3000);
     }

【讨论】:

  • 为什么我们需要第二个 else if,因为它是空的?
猜你喜欢
  • 2014-12-30
  • 2016-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多