0

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

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

在此处输入图像描述

我试过这个,

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

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

谁能帮我?

4

1 回答 1

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();

使用 name 或 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();`
于 2012-11-27T09:53:50.287 回答