위메프 타임딜 공략!

import java.util.List;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Wemakeprice {
	public static void main(String[] args)  throws Exception {
		System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
		ChromeOptions option = new ChromeOptions();
//		option.addArguments("headless"); // 헤드레스 모드, 창없이 내부적으로 처리
//		option.addArguments("window-size:1920,1080");
		option.addArguments("--blink-setting=imagesEnable=false"); // 페이지 로딩에서 이미지 제외
		WebDriver driver = new ChromeDriver(option);


		WebDriverWait wait = new WebDriverWait(driver,10);
		JavascriptExecutor js = (JavascriptExecutor)driver;
		long startTime = System.currentTimeMillis();

		// 로그인 페이지 -----------------------------------------------
		driver.get("https://front.wemakeprice.com/user/login?returnUrl=https%3A%2F%2Ffront.wemakeprice.com%2Fmain&type=GENERAL&orderYN=N&selectionYN=N");
		WebElement idBox = driver.findElement(By.id("_loginId"));
		WebElement pwBox = driver.findElement(By.id("_loginPw"));
		js.executeScript("arguments[0].value=arguments[1]", idBox, Account.wid);
		js.executeScript("arguments[0].value=arguments[1]", pwBox, Account.wpw);

		driver.findElement(By.id("_userLogin")).click();

		wait.until(ExpectedConditions.urlToBe("https://front.wemakeprice.com/user/password/advice"));
		// 이벤트 페이지로 이동 -------------------------------------------

		driver.get("https://front.wemakeprice.com/promotion/3693");
		String defaultWindow = driver.getWindowHandle(); // 윈도우핸들값 저장

		// 첫번째 썸네일 클릭
		List<WebElement> product_btns = driver.findElements(By.className("flag_wrap"));
		product_btns.get(2).click();

		//이미지 alt값 불러와서 비교 - if문 안에서 클릭 안된다?
		//		List<WebElement> thumbs = driver.findElements(By.cssSelector(".item_img img"));
		//		System.out.println(thumbs.size());
		//		for(WebElement w : thumbs) {
		//			if(w.getAttribute("alt").contains("젤다의 전설 꿈")) {
		//				System.out.println(w.getAttribute("alt"));
		//				w.click();
		//				break;
		//			}
		//		}


		Set<String> hwnds = driver.getWindowHandles();
		//		System.out.println("Default hwnd : "+defaultWindow);
		for(String hwnd : hwnds) {
			driver.switchTo().window(hwnd);
			//			System.out.println(hwnd);
		}
		//구매하기 버튼 누르기
		List<WebElement> buy_btns = driver.findElements(By.className("buy"));
		buy_btns.get(0).click();
				
		//결제하기 - 실행안됨, 대기 명령
	
		wait.until(ExpectedConditions.elementToBeClickable(By.id("btnPaymentSubmit")));
		
		driver.findElement(By.id("btnPaymentSubmit")).click();

		//overlay_subWapper (카카오페이 결제창, iframe) 가 나타날 때까지 대기
//		wait.until(ExpectedConditions.elementToBeClickable(By.className("link_gnb")));
		Thread.sleep(5000);
		driver.findElement(By.className("link_gnb")).click();
		// id = userPhone
		// = userBirth
		// Button class=btn_payask
		
		long endTime = System.currentTimeMillis();
		System.out.println( endTime - startTime );

		Thread.sleep(60000);
		driver.close(); 
	}
}

해상도가 풀 해상도가 아니라서 클릭이 안먹히는 경우도 있음

option.addArguments("window-size:1920,1080");

로그인 페이지

		// 로그인 페이지 -----------------------------------------------
		driver.get("https://front.wemakeprice.com/user/login?returnUrl=https%3A%2F%2Ffront.wemakeprice.com%2Fmain&type=GENERAL&orderYN=N&selectionYN=N");
		WebElement idBox = driver.findElement(By.id("_loginId"));
		WebElement pwBox = driver.findElement(By.id("_loginPw"));
		js.executeScript("arguments[0].value=arguments[1]", idBox, Account.wid);
		js.executeScript("arguments[0].value=arguments[1]", pwBox, Account.wpw);

		driver.findElement(By.id("_userLogin")).click();

		wait.until(ExpectedConditions.urlToBe("https://front.wemakeprice.com/user/password/advice"));
		

이벤트 페이지

		// 이벤트 페이지로 이동 -------------------------------------------

		driver.get("https://front.wemakeprice.com/promotion/3693");
		String defaultWindow = driver.getWindowHandle(); // 윈도우핸들값 저장

썸네일 선택

		List<WebElement> product_btns = driver.findElements(By.className("flag_wrap"));
		product_btns.get(2).click();

새 탭으로 이동

		Set<String> hwnds = driver.getWindowHandles();
		//		System.out.println("Default hwnd : "+defaultWindow);
		for(String hwnd : hwnds) {
			driver.switchTo().window(hwnd);
			//			System.out.println(hwnd);
		}

구매 버튼 누르기

		List<WebElement> buy_btns = driver.findElements(By.className("buy"));
		buy_btns.get(0).click();

결제하기

		wait.until(ExpectedConditions.elementToBeClickable(By.id("btnPaymentSubmit")));
		driver.findElement(By.id("btnPaymentSubmit")).click();

카카오페이 결제창, iframe


신한 결제창으로 변경

import java.util.List;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Wemakeprice {
	public static void main(String[] args)  throws Exception {
		System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
		ChromeOptions option = new ChromeOptions();
		option.addArguments("--blink-setting=imagesEnable=false"); // 페이지 로딩에서 이미지 제외
		WebDriver driver = new ChromeDriver(option);


		WebDriverWait wait = new WebDriverWait(driver,10);
		JavascriptExecutor js = (JavascriptExecutor)driver;
		long startTime = System.currentTimeMillis();

		// 로그인 페이지 -----------------------------------------------
		driver.get("https://front.wemakeprice.com/user/login?returnUrl=https%3A%2F%2Ffront.wemakeprice.com%2Fmain&type=GENERAL&orderYN=N&selectionYN=N");
		WebElement idBox = driver.findElement(By.id("_loginId"));
		WebElement pwBox = driver.findElement(By.id("_loginPw"));
		js.executeScript("arguments[0].value=arguments[1]", idBox, Account.wid);
		js.executeScript("arguments[0].value=arguments[1]", pwBox, Account.wpw);

		driver.findElement(By.id("_userLogin")).click();

		wait.until(ExpectedConditions.urlToBe("https://front.wemakeprice.com/user/password/advice"));
		// 이벤트 페이지로 이동 -------------------------------------------

		driver.get("https://front.wemakeprice.com/promotion/3693");
		String defaultWindow = driver.getWindowHandle(); // 윈도우핸들값 저장

		// 첫번째 썸네일 클릭
		List<WebElement> product_btns = driver.findElements(By.className("flag_wrap"));
		product_btns.get(1).click();

		Set<String> hwnds = driver.getWindowHandles();
		//		System.out.println("Default hwnd : "+defaultWindow);
		for(String hwnd : hwnds) {
			driver.switchTo().window(hwnd);
			//			System.out.println(hwnd);
		}
		//구매하기 버튼 누르기
		List<WebElement> buy_btns = driver.findElements(By.className("buy"));
		buy_btns.get(0).click();

		//할부 일시불 선택
		wait.until(ExpectedConditions.elementToBeClickable(By.id("installment")));
		driver.findElement(By.id("installment")).click();
		List<WebElement> installmentList = driver.findElements(By.cssSelector("#installmentList>li"));
		installmentList.get(0).click();

		//결제하기
		driver.findElement(By.id("btnPaymentSubmit")).click();

		//iframe 나타날 때까지 대기
		//		driver.switchTo().frame(0);
		//		wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".cont1>.btn")));
		//		driver.findElement(By.cssSelector(".cont1>.btn")).click();

		long endTime = System.currentTimeMillis();
		System.out.println( endTime - startTime );

		Thread.sleep(60000);
		driver.close(); 
	}
}

getWindowHandles

운영체제에서 부여한 윈도우별 id : 핸들값 핸들id


import java.util.List;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Quiz02 {
	public static void main(String[] args)  throws Exception {
		System.setProperty("webdriver.chrome.driver", "chromedriver.exe");

		ChromeOptions option = new ChromeOptions();
		//		option.addArguments("headless"); // 헤드레스 모드, 창없이 내부적으로 처리
		//		option.addArguments("--blink-setting=imagesEnable=false"); // 페이지 로딩에서 이미지 제외
		//		WebDriver driver = new ChromeDriver(option);

		WebDriver driver = new ChromeDriver();
		WebDriverWait wait = new WebDriverWait(driver,10);
		JavascriptExecutor js = (JavascriptExecutor)driver;

		long startTime = System.currentTimeMillis();

		driver.get("https://front.wemakeprice.com/promotion/3693");
		String defaultWindow = driver.getWindowHandle(); // 윈도우핸들값 저장

		List<WebElement> product_btns = driver.findElements(By.className("flag_wrap"));
		product_btns.get(0).click();

		// 운영체제가 윈도우 별로 id를 부여 : 핸들값 핸들id
		// getWindowHandles : 핸들값을 가져와라
		Set<String> hwnds = driver.getWindowHandles();
		//set 은 순서가 없으므로 foreach로 출력
		System.out.println("Default hwnd : "+defaultWindow);
		for(String hwnd : hwnds) {
			System.out.println(hwnd);
			if(!hwnd.contentEquals(defaultWindow)) { //변경
				driver.switchTo().window(hwnd);
				break;
			}
		}

		List<WebElement> buy_btns = driver.findElements(By.className("btn_sys"));
		buy_btns.get(0).click();

		long endTime = System.currentTimeMillis();
		System.out.println( endTime - startTime );

		Thread.sleep(5000);
		driver.close(); 
	}
}

 

defaultWindow 저장

String defaultWindow = driver.getWindowHandle(); // 윈도우핸들값 저장
		// 운영체제가 윈도우 별로 id를 부여 : 핸들값 핸들id
		// getWindowHandles : 핸들값을 가져와라
		Set<String> hwnds = driver.getWindowHandles();
		System.out.println("Default hwnd : "+defaultWindow);
        
		//set 은 순서가 없으므로 foreach로 출력
		for(String hwnd : hwnds) {
			System.out.println(hwnd);
			if(!hwnd.contentEquals(defaultWindow)) { //변경
				driver.switchTo().window(hwnd);
				break;
			}
		}

새탭으로 이동

import java.util.List;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Quiz02 {
	public static void main(String[] args)  throws Exception {
		System.setProperty("webdriver.chrome.driver", "chromedriver.exe");

		ChromeOptions option = new ChromeOptions();
		WebDriver driver = new ChromeDriver();
		WebDriverWait wait = new WebDriverWait(driver,10);
		JavascriptExecutor js = (JavascriptExecutor)driver;

		long startTime = System.currentTimeMillis();

		driver.get("https://front.wemakeprice.com/promotion/3693");
		String defaultWindow = driver.getWindowHandle(); // 윈도우핸들값 저장

		List<WebElement> product_btns = driver.findElements(By.className("flag_wrap"));
		product_btns.get(0).click();

		// 운영체제가 윈도우 별로 id를 부여 : 핸들값 핸들id
		// getWindowHandles : 핸들값을 가져와라
		Set<String> hwnds = driver.getWindowHandles();
		//set 은 순서가 없으므로 foreach로 출력
		System.out.println("Default hwnd : "+defaultWindow);
		for(String hwnd : hwnds) {
			driver.switchTo().window(hwnd);
			System.out.println(hwnd);
		}

		List<WebElement> buy_btns = driver.findElements(By.className("btn_sys"));
		buy_btns.get(0).click();

		long endTime = System.currentTimeMillis();
		System.out.println( endTime - startTime );

		Thread.sleep(5000);
		driver.close(); 
	}
}
		ChromeOptions option = new ChromeOptions();
		option.addArguments("headless"); // 헤드리스 모드, 창없이 내부적으로 처리
		option.addArguments("--blink-setting=imagesEnable=false"); // 페이지 로딩에서 이미지 제외
		WebDriver driver = new ChromeDriver(option);

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Quiz02 {
	public static void main(String[] args)  throws Exception {
		System.setProperty("webdriver.chrome.driver", "chromedriver.exe");

		ChromeOptions option = new ChromeOptions();
		//		option.addArguments("headless"); // 헤드레스 모드, 창없이 내부적으로 처리
		//		option.addArguments("--blink-setting=imagesEnable=false"); // 페이지 로딩에서 이미지 제외
		//		WebDriver driver = new ChromeDriver(option);

		WebDriver driver = new ChromeDriver();
		WebDriverWait wait = new WebDriverWait(driver,10);
		JavascriptExecutor js = (JavascriptExecutor)driver;


		long startTime = System.currentTimeMillis();


		driver.get("https://front.wemakeprice.com/promotion/3693");


		List<WebElement> product_btns = driver.findElements(By.className("flag_wrap"));
		product_btns.get(0).click();
		//새 탭으로 이동 
		//팝업제어
//		driver.switchTo().window();
		
		


		List<WebElement> buy_btns = driver.findElements(By.className("btn_sys"));
		buy_btns.get(0).click();



		long endTime = System.currentTimeMillis();

		System.out.println( endTime - startTime );

		Thread.sleep(5000);
		driver.close(); 
	}
}

동작 시간 측정

		long startTime = System.currentTimeMillis();
        //     작업 수행 
        long endTime = System.currentTimeMillis();
		System.out.println( endTime - startTime );

이미지 로딩 제외

		ChromeOptions option = new ChromeOptions();
		option.addArguments("--blink-setting=imagesEnable=false"); // 페이지 로딩에서 이미지 제외
		WebDriver driver = new ChromeDriver(option);

헤드리스 모드

창없이 실행 , 고스트모드의 발전

		ChromeOptions option = new ChromeOptions();
		option.addArguments("headless"); // 헤드레스 모드, 창없이 내부적으로 처리
		option.addArguments("--blink-setting=imagesEnable=false"); // 페이지 로딩에서 이미지 제외
		WebDriver driver = new ChromeDriver(option);

팝업 창 제어

새탭 제어

 

'디지털 컨버전스 > Selenium' 카테고리의 다른 글

[Selenium] 타임딜  (0) 2020.04.22
[Selenium] 팝업창 제어  (0) 2020.04.22
[Selenium] iframe - 내게쓰기 입력  (0) 2020.04.22
[Selenium] 자바스크립트 입력 - 로그인  (0) 2020.04.22
[Selenium] 명시적 대기  (0) 2020.04.22

iframe

 

웹 어플리케이션 적용 : 에디터...

보안 문제 때문에 거부될 수도 있음

 

드라이버에서는 내부의 iframe을 찾을수 없음

포커스를 이동시켜야 함


switchTo()

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class quiz01 {
	public static void main(String[] args)  throws Exception {
		System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		WebDriverWait wait = new WebDriverWait(driver,10);
		JavascriptExecutor js = (JavascriptExecutor)driver;
		
		driver.get("https://logins.daum.net/accounts/signinform.do?url=https%3A%2F%2Fwww.daum.net%2F");
		driver.findElement(By.id("id")).sendKeys(Account.id);
		driver.findElement(By.id("inputPwd")).sendKeys(Account.pw);
		driver.findElement(By.id("loginBtn")).click();
		wait.until(ExpectedConditions.urlToBe("https://www.daum.net/"));
		driver.get("https://mail.daum.net/");

		//내가 한 방법 : 메일 이름들이 로딩 될때 까지 대기 , 더 느린듯
		//wait.until(ExpectedConditions.presenceOfElementLocated(By.className("tit_subject")));

		// globalIndicator - 한번 사라짐
		wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("globalIndicator")));
		//globalIndicator를 활용한 다른 코드
		//wait.until(ExpectedConditions.attributeToBe(By.id("globalIndicator"), "display", "none"));
		List<WebElement> btns = driver.findElements(By.className("btn_my"));
		btns.get(0).click();
		
		
		
		wait.until(ExpectedConditions.presenceOfElementLocated(By.id("mailSubject"))).sendKeys("제목~~~~~~~~~~!!!");
		
		driver.switchTo().frame("tx_canvas_wysiwyg"); // 포커스를 아이프레임으로
		
		driver.findElement(By.tagName("body")).sendKeys("내~~~~~~~~용~~~~~~~~~~~");
		
		
		
		Thread.sleep(5000);
		driver.close(); 
	}
}

보내기 버튼은 외부프레임

 

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class quiz01 {
	public static void main(String[] args)  throws Exception {
		System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		WebDriverWait wait = new WebDriverWait(driver,10);
		JavascriptExecutor js = (JavascriptExecutor)driver;
		
		driver.get("https://logins.daum.net/accounts/signinform.do?url=https%3A%2F%2Fwww.daum.net%2F");
		driver.findElement(By.id("id")).sendKeys(Account.id);
		driver.findElement(By.id("inputPwd")).sendKeys(Account.pw);
		driver.findElement(By.id("loginBtn")).click();
		wait.until(ExpectedConditions.urlToBe("https://www.daum.net/"));
		driver.get("https://mail.daum.net/");

		//내가 한 방법 : 메일 이름들이 로딩 될때 까지 대기 , 더 느린듯
		//wait.until(ExpectedConditions.presenceOfElementLocated(By.className("tit_subject")));

		// globalIndicator - 한번 사라짐
		wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("globalIndicator")));
		//globalIndicator를 활용한 다른 코드
		//wait.until(ExpectedConditions.attributeToBe(By.id("globalIndicator"), "display", "none"));
		List<WebElement> btns = driver.findElements(By.className("btn_my"));
		btns.get(0).click();
		
		
		
		wait.until(ExpectedConditions.presenceOfElementLocated(By.id("mailSubject"))).sendKeys("제목~~~~~~~~~~!!!");
		
		driver.switchTo().frame("tx_canvas_wysiwyg"); // 포커스를 아이프레임으로
		driver.findElement(By.tagName("body")).sendKeys("내~~~~~~~~용~~~~~~~~~~~");
		
		driver.switchTo().defaultContent(); // 포커스를 기본값으로
		List<WebElement> btnWrite= driver.findElements(By.cssSelector(".wrap_toolbar .btn_write"));
		btnWrite.get(0).click();
		
		Thread.sleep(5000);
		driver.close(); 
	}
}

JavascriptExecutor

... 가변인자

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Exam04 {
	public static void main(String[] args)  throws Exception {
		System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
		WebDriver driver = new ChromeDriver();
//		WebDriverWait wait = new WebDriverWait(driver,10);
		JavascriptExecutor js = (JavascriptExecutor)driver;

		driver.get("https://nid.naver.com/nidlogin.login?mode=form&url=https%3A%2F%2Fwww.naver.com");

		WebElement idBox = driver.findElement(By.id("id"));
		WebElement pwBox = driver.findElement(By.id("pw"));
		js.executeScript("arguments[0].value=arguments[1]", idBox, Account.nid);
		js.executeScript("arguments[0].value=arguments[1]", pwBox, Account.npw);
		// idBox에다가 ABCD를 value값으로 넣어라
		// 빠른 타이핑이 아니라 붙여넣는 효과
		// send key는 봇이라고 감지하지만 자바스크립트로 붙여넣는 것은 감지 못함
		
		driver.findElement(By.id("log.login")).click();
		
		
		Thread.sleep(5000);
		driver.close(); 
	}
}

		js.executeScript("arguments[0].value=arguments[1]", idBox, Account.nid);

idBox에다가 ABCD를 value값으로 넣어라 빠른 타이핑이 아니라 붙여넣는 효과
send key는 봇이라고 감지하지만 자바스크립트로 붙여넣는 것은 감지 못함

다음 사이트 로그인 후 메일로 이동하고자 함

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class quiz01 {
	public static void main(String[] args)  throws Exception {
		System.setProperty("webdriver.chrome.driver", "chromedriver.exe");

		WebDriver driver = new ChromeDriver();
		driver.get("https://logins.daum.net/accounts/signinform.do?url=https%3A%2F%2Fwww.daum.net%2F");

		driver.findElement(By.id("id")).sendKeys(Account.id);
		driver.findElement(By.id("inputPwd")).sendKeys(Account.pw);
		driver.findElement(By.id("loginBtn")).click();
        
		driver.get("https://mail.daum.net/"); // 로그아웃 된 후 이동

		Thread.sleep(5000);
		driver.close(); 

	}
}

 

문제점 : 페이지 이동시 로그아웃

 

이유 : 로그인은 서버와 데이터를 부고 받아서 이루어진다. 코드 동기화 문제

1) 로그인 처리가 완료되지 않은 상태로 이동

2) 로그인 처리 중이라 이동명령 무시

 

따라서 로그인 명령을 내린후 로그인이 처리될 시간을 줘야함

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class quiz01 {
	public static void main(String[] args)  throws Exception {
		System.setProperty("webdriver.chrome.driver", "chromedriver.exe");

		WebDriver driver = new ChromeDriver();
		driver.get("https://logins.daum.net/accounts/signinform.do?url=https%3A%2F%2Fwww.daum.net%2F");

		driver.findElement(By.id("id")).sendKeys(Account.id);
		driver.findElement(By.id("inputPwd")).sendKeys(Account.pw);

		driver.findElement(By.id("loginBtn")).click();
		
		Thread.sleep(1000); //로그인이 처리될 시간
		driver.get("https://mail.daum.net/");

		Thread.sleep(5000);
		driver.close(); 

	}
}

Thread.sleep(1000);

불리한 경우

1) 트랙픽이 몰리면 1초로는 부족할 수도 있다.

2) 1초보다 빨리 로그인이 수행될 경우 남은 시간을 기다려야함


WebDriverWait

웹드라이버에 대기 시간을 주는 객체

		WebDriverWait wait = new WebDriverWait(driver,10); // 10초
wait.until(ExpectedConditions.)

 

명시적 대기

조건이 충족 될때 까지 대기

wait.until(ExpectedConditions.urlToBe("https://www.daum.net/")); //명시적 대기

묵시적 대기

Thread.sleep(5000); //묵시적 대기

내게 쓰기

1) url로 갈수 있는가?

2) url로 갈 수 없다면 눌러줘야함

 

클래스로 선택시 몇개가 나오는지 확인

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class quiz01 {
	public static void main(String[] args)  throws Exception {
		System.setProperty("webdriver.chrome.driver", "chromedriver.exe");

		WebDriver driver = new ChromeDriver();
		WebDriverWait wait = new WebDriverWait(driver,10);
		driver.get("https://logins.daum.net/accounts/signinform.do?url=https%3A%2F%2Fwww.daum.net%2F");

		driver.findElement(By.id("id")).sendKeys(Account.id);
		driver.findElement(By.id("inputPwd")).sendKeys(Account.pw);
		driver.findElement(By.id("loginBtn")).click();
		
		wait.until(ExpectedConditions.urlToBe("https://www.daum.net/"));
		
		driver.get("https://mail.daum.net/");

		List<WebElement> btns = driver.findElements(By.className("btn_my"));
		System.out.println(btns.size()); // 1
		
		Thread.sleep(5000);
		driver.close(); 

	}
}

 


import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class quiz01 {
	public static void main(String[] args)  throws Exception {
		System.setProperty("webdriver.chrome.driver", "chromedriver.exe");

		WebDriver driver = new ChromeDriver();
		WebDriverWait wait = new WebDriverWait(driver,10);
		driver.get("https://logins.daum.net/accounts/signinform.do?url=https%3A%2F%2Fwww.daum.net%2F");

		driver.findElement(By.id("id")).sendKeys(Account.id);
		driver.findElement(By.id("inputPwd")).sendKeys(Account.pw);
		driver.findElement(By.id("loginBtn")).click();
		
		wait.until(ExpectedConditions.urlToBe("https://www.daum.net/"));
		
		driver.get("https://mail.daum.net/");

		List<WebElement> btns = driver.findElements(By.className("btn_my"));
		
		btns.get(0).click();

		Thread.sleep(5000);
		driver.close(); 
	}
}

 

실행되지 않을시

서버의 승인전에 실행되서 실패했을 가능성 확인

묵시적 대기(Thread.sleep)로 테스트

public class quiz01 {
	public static void main(String[] args)  throws Exception {
		System.setProperty("webdriver.chrome.driver", "chromedriver.exe");

		WebDriver driver = new ChromeDriver();
		WebDriverWait wait = new WebDriverWait(driver,10);
		driver.get("https://logins.daum.net/accounts/signinform.do?url=https%3A%2F%2Fwww.daum.net%2F");

		driver.findElement(By.id("id")).sendKeys(Account.id);
		driver.findElement(By.id("inputPwd")).sendKeys(Account.pw);
		driver.findElement(By.id("loginBtn")).click();
		
		wait.until(ExpectedConditions.urlToBe("https://www.daum.net/"));
		driver.get("https://mail.daum.net/");
		
		Thread.sleep(2000); // 테스트용 대기 시간 2초
		List<WebElement> btns = driver.findElements(By.className("btn_my"));	
		btns.get(0).click();

		Thread.sleep(5000);
		driver.close(); 
	}
}

Thread.sleep 대신에

elementToBeClickable

public class quiz01 {
	public static void main(String[] args)  throws Exception {
		System.setProperty("webdriver.chrome.driver", "chromedriver.exe");

		WebDriver driver = new ChromeDriver();
		WebDriverWait wait = new WebDriverWait(driver,10);
		driver.get("https://logins.daum.net/accounts/signinform.do?url=https%3A%2F%2Fwww.daum.net%2F");

		driver.findElement(By.id("id")).sendKeys(Account.id);
		driver.findElement(By.id("inputPwd")).sendKeys(Account.pw);
		driver.findElement(By.id("loginBtn")).click();
		
		wait.until(ExpectedConditions.urlToBe("https://www.daum.net/"));
		
		driver.get("https://mail.daum.net/");
		
        //클릭 가능 할때까지 대기
		wait.until(ExpectedConditions.elementToBeClickable(By.className("btn_my")));

		List<WebElement> btns = driver.findElements(By.className("btn_my"));
		btns.get(0).click();

		Thread.sleep(5000);
		driver.close(); 
	}
}

실패 : 추가 분석 필요

 

메일 페이지 로딩

메일로딩 인디케이터가 덮음

인티케이터 지워짐


By.xpath

성능이 좋진 않지만 최후의 수단

네이버 웹툰 페이지에서 이름 찾기

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class Exam03 {
	public static void main(String[] args) throws Exception{

		System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.get("https://comic.naver.com/webtoon/weekdayList.nhn?week=");
		
		List<WebElement> thumbs = driver.findElements(By.cssSelector(".thumb a"));
		System.out.println(thumbs.size()); // 찾은 개수 : 50
		
		Thread.sleep(5000);
		driver.close(); 
	}
}
cssSelector(".thumb a")
// css 선택자 문법 사용 가능

for문으로 정보값 출력

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class Exam03 {
	public static void main(String[] args) throws Exception{

		System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.get("https://comic.naver.com/webtoon/weekdayList.nhn?week=");
		
		List<WebElement> thumbs = driver.findElements(By.cssSelector(".thumb a"));
		System.out.println(thumbs.size());
		
		for(WebElement w : thumbs) {
			System.out.println(w.getAttribute("title"));
		}
		
		Thread.sleep(5000);
		driver.close(); 
	}
}

50개의 title 출력


title이 "여신강림"일 때 클릭

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class Exam03 {
	public static void main(String[] args) throws Exception{

		System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.get("https://comic.naver.com/webtoon/weekdayList.nhn?week=");
		
		List<WebElement> thumbs = driver.findElements(By.cssSelector(".thumb a"));
		System.out.println(thumbs.size());
		
		for(WebElement w : thumbs) {
			if(w.getAttribute("title").contentEquals("여신강림")) {
				w.click();
			}
		}
		
		Thread.sleep(5000);
		driver.close(); 
	}
}

 

네이버 로그인

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class Exam02 {
	public static void main(String[] args)  throws Exception {
		System.setProperty("webdriver.chrome.driver", "chromedriver.exe");

		WebDriver driver = new ChromeDriver();
		driver.get("https://nid.naver.com/nidlogin.login?mode=form&url=https%3A%2F%2Fwww.naver.com");//
		
		driver.findElement(By.id("id")).sendKeys("ABCD");
		driver.findElement(By.id("pw")).sendKeys("password");
		
//		driver.findElement(By.id("log.login")).click(); // 네이버에서 아이피밴 받을 수도있다. + 네이버에선 자동화툴 차단 기능 있음
			
		Thread.sleep(5000);
		driver.close(); 	
	}
}

1) 느리게 설정 - 자동화툴 쓰는 의미 없음

2) 우회책

Ctrl + C

Ctrl + V

로 입력하면 된다.

 


다음 로그인

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class quiz01 {
	public static void main(String[] args)  throws Exception {
		System.setProperty("webdriver.chrome.driver", "chromedriver.exe");

		WebDriver driver = new ChromeDriver();
		driver.get("https://logins.daum.net/accounts/signinform.do?url=https%3A%2F%2Fwww.daum.net%2F");

		driver.findElement(By.id("id")).sendKeys(Account.id);
		driver.findElement(By.id("inputPwd")).sendKeys(Account.pw);

		driver.findElement(By.id("loginBtn")).click();

		Thread.sleep(5000);
		driver.close(); 

	}
}
public class Account {
	public static final String id ="";
	public static final String pw ="";
}

 

+ Recent posts