【问题标题】:How can I give input to GWT FileUpload using Selenium WebDriver?如何使用 Selenium WebDriver 向 GWT FileUpload 提供输入?
【发布时间】:2012-11-27 08:51:15
【问题描述】:

我需要在 GWT FileUpload 中提供一个输入文本文件并使用 Selenium Webdriver 对其进行测试。

我有上传小部件,我需要提供一个文本文件作为输入。然后单击一个上传按钮来执行我的功能。

我试过了,

driver.findElement(By.id("uploadField")).sendKeys("C:/Desktop/Input.txt");
driver.findElement(By.id("uploadButton")).click();

但我无法为此上传字段提供输入。

谁能帮帮我?

【问题讨论】:

  • 请展示您正在尝试什么以及您面临的问题是什么?
  • 他正试图从 Selenium 调用系统文件浏览器。这在 Selenium 1 中是不可能的。
  • Ok 那我该如何实现呢?我在 Selenium2 WebDriver 中工作

标签: gwt selenium webdriver selenium-webdriver


【解决方案1】:

查看此代码对我有用,请参阅:在 sendkeys 中使用正确的文件路径

         driver.get("http://www.freepdfconvert.com/");
     driver.findElement(By.id("UploadedFile")).sendKeys("C:\\Users\\username\\Downloads\\HP1.pdf");      
     try {
         Thread.sleep(4000);
        } 
         catch (Exception e) {}
     driver.findElement(By.name("pdfsubmit")).click();
        }

  driver.findElement(By.id("uploadField")).sendKeys("C:/xyz.txt");
  driver.findElement(By.name("uploadButton")).click();

使用名称或 Xpath 然后检查。

编辑

是的,它适用于所有浏览器,但对于 IE 和 chrome,您必须添加小代码。 即:

DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer(); 
           ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
             WebDriver driver = new InternetExplorerDriver(ieCapabilities);

    File file = new File("E://chromedriver.exe");
    System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
    WebDriver driver = new InternetExplorerDriver();`

【讨论】:

  • 它的工作。谢谢。错误是我使用'/'而不是'\',即'\\'。我现在更新为'\\'。它的工作。很好的答案。谢谢。
猜你喜欢
  • 1970-01-01
  • 2014-06-29
  • 1970-01-01
  • 1970-01-01
  • 2015-02-28
  • 2013-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多