【问题标题】:How to upload a file in SafariDriver / Safari using selenium webdriver?如何使用 selenium webdriver 在 SafariDriver / Safari 中上传文件?
【发布时间】:2015-06-11 14:01:23
【问题描述】:

您好,您能帮我在 Safari 和 Mac OS 中上传文件吗?

我提供了一个网站,我们可以通过单击“选择文件”按钮上传以下类型的文件(JPG、PNG、GIF、DOC、XLS、PDF、ZIP、RAR、ZIPX)

package Default;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.safari.SafariDriver;

public class Safari_demo {

    public static void main(String[] args) throws InterruptedException{

        WebDriver driver = new SafariDriver();

        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        Thread.sleep(6000);

        driver.get("http://www.files.com/");

        driver.findElement(By.id("uploadFields")).click();

        //can you please help me with code in this line 
        //this is the place were i need to enter the address of the
        //file which is in my system  

        driver.close();
    }

}

【问题讨论】:

  • driver.findElement(By.id("youruploadbuttonid")).sendKeys("yourpicturelocalpath");
  • 你能解决这个问题吗?

标签: java macos selenium safari


【解决方案1】:

SafariDriver 好像不支持文件上传:https://code.google.com/p/selenium/issues/detail?id=4220

【讨论】:

    【解决方案2】:

    试试这个。

    fd.findElement(By.xpath(".//*[@id='uploadFields']/input"))
                    .sendKeys(
                            "your image path");
    

    【讨论】:

      【解决方案3】:

      所以文件上传对话框实际上是一个本地系统对话框而不是网络对话框,因此您不能使用 Selenium 与之交互。本地实现的方法是将路径发送到 filePath 文本框,saucelabs 描述得很好:

      对于那些在本地执行此操作的人,您需要做的就是使用 sendKeys 命令在任何文件字段中键入文件的本地路径。 这对所有驱动程序都很有吸引力。将此测试移至 远程服务器(例如,我们的 Selenium 2 Cloud),所有你 需要做的是使用 setFileDetector 方法让 WebDriver 知道 您正在将文件从本地计算机上传到远程 服务器,而不仅仅是输入路径。几乎神奇的是,该文件将 通过 JSON Wire 进行 base64 编码和透明发送 在编写固定的远程路径之前为您准备的协议。这是一 出色的解决方案,因为它可以让您将测试从本地切换到 远程驱动程序,而不必担心更改测试代码。

      https://saucelabs.com/resources/selenium-file-upload

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-23
        • 1970-01-01
        • 2013-05-29
        • 2023-03-06
        • 1970-01-01
        • 1970-01-01
        • 2014-01-22
        • 1970-01-01
        相关资源
        最近更新 更多