Selenium에서 브라우저 쿠키를 지울 수 있습니다. deleteCookieNamed 메소드 특정 이름의 쿠키를 삭제합니다. 이름이 지정된 쿠키는 메서드에 대한 인수로 전달됩니다. 먼저 쿠키를 추가한 다음 가져오고 마지막으로 삭제합니다.
구문
driver.manage().deleteCookieNamed("foo");
deleteAllCookies 삭제라는 또 다른 방법 기존 도메인의 모든 쿠키. 먼저 쿠키를 추가한 다음 쿠키를 가져와서 삭제합니다.
구문
driver.manage().deleteAllCookies();
예
import java.util.Set; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class DeleteCookiesViaName{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("https://www.tutorialspoint.com/index.htm"); // wait of 4 seconds driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS); // setting name and value for cookie Cookie c = new Cookie("test", "selenium"); Cookie r = new Cookie("subject", "Java"); // cookie addition driver.manage().addCookie(c); driver.manage().addCookie(r); // obtain the cookies Set<Cookie> ck = driver.manage().getCookies(); //iterate through the cookies for (Cookie cookie : ck) { System.out.println("Cookie Name : "+cookie.getName()); System.out.println("Cookie Value : "+cookie.getValue()); // delete cookies by name driver.manage().deleteCookieNamed(cookie.getName()); } // obtain the cookies after delete Set ch = driver.manage().getCookies(); System.out.println("Cookie count after delete: "+ch.size()); } }
예
deleteAllCookies를 사용한 코드 구현.
import java.util.Set; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class DeleteCookiesAll{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("https://www.tutorialspoint.com/index.htm"); // wait of 4 seconds driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS); // setting name and value for cookie Cookie c = new Cookie("test", "selenium"); Cookie r = new Cookie("subject", "Java"); // cookie addition driver.manage().addCookie(c); driver.manage().addCookie(r); // obtain the cookies Set<Cookie> ck = driver.manage().getCookies(); //iterate through the cookies for (Cookie cookie : ck) { System.out.println("Cookie Name : "+cookie.getName()); System.out.println("Cookie Value : "+cookie.getValue()); } // delete cookies driver.manage().deleteAllCookies(); // obtain the cookies after delete Set ch = driver.manage().getCookies(); System.out.println("Cookie count after delete: "+ch.size()); } }
출력