본문 바로가기

테스트툴/Selenium

Selenium 파일 업로드

실습은 

 

File Upload Demo

 

demo.guru99.com

위 페이지를 활용하였다

 

파일 선택 버튼을 찾아 sendKeys() 메서드로 내가 원하는 경로의 파일을 업로드한다

package newpackage;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebElement;//webElement 사용
import org.openqa.selenium.support.ui.Select;//Select 사용
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;//Action 사용

public class MyClass {
	public static void main(String[] args){
		System.setProperty("webdriver.chrome.driver", "c:/selenium/chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		
		String getURL = "http://demo.guru99.com/test/upload/";
		driver.get(getURL);
		
		WebElement uploadElement = driver.findElement(By.name("uploadfile_0"));
		uploadElement.sendKeys("원하는 파일 경로를 넣으시면 됩니다");//따옴표 안에 파일경로 입력
		
		driver.findElement(By.id("terms")).click();
		
		driver.findElement(By.name("send")).click();
	}
}

위 코드로

성공적으로 파일을 업로드했음을 확인할 수 있다

 

별개로 파일을 다운로드하려면 WebDriver에는 다운로드 링크나 다운로드 대화 상자에 액세스 할 수 있는 기능이 없다

wget이라는 별도의 프로그램을 사용하여 다운로드를 자동화하는 데 사용할 수 있다