File Upload using Selenium WebDriver and Java Robot Class

后端 未结 7 1861
说谎
说谎 2020-11-28 07:19

I am using Selenium WebDriver and Java and I need to automate the file upload feature. I tried a lot, but the moment the Browse button is clicked and a new window opens the

7条回答
  •  鱼传尺愫
    2020-11-28 07:50

    This should work with Firefox, Chrome and IE drivers.

    FirefoxDriver driver = new FirefoxDriver();
    
    driver.get("http://localhost:8080/page");
    
    File file = null;
    
    try {
        file = new File(YourClass.class.getClassLoader().getResource("file.txt").toURI());
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
    
    Assert.assertTrue(file.exists()); 
    
    WebElement browseButton = driver.findElement(By.id("myfile"));
    browseButton.sendKeys(file.getAbsolutePath());
    

提交回复
热议问题