2012-11-27 19 views
0

我需要给GWT输入文本文件FileUpload并使用Selenium Webdriver进行测试。如何使用Selenium WebDriver为GWT FileUpload提供输入?

我有上传小部件,在那我需要给一个文本文件作为输入。然后点击上传按钮来完成我的功能。

enter image description here

我想这一点,

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

,但我不能给输入到该上传字段。

任何人都可以帮助我吗?

+0

PLZ表明你正在尝试的,什么是您所面临的问题? –

+0

他正试图从Selenium调用系统文件浏览器。这在Selenium 1中是不可能的。 – SSR

+0

好那我该怎么做到这一点?我在Selenium2 WebDriver – Prince

回答

2

看到这个代码工作对我来说看到:在的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必须添加小的代码。 IE:

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();` 
+0

@Prince这个代码适用于你吗?让我知道 –

+0

它的工作。谢谢。错误是我用'/'代替'\',即'\\'。我现在更新为'\\'。它的工作。很好的答案。谢谢。 – Prince

+0

祝你好运.............. :) –

相关问题