使用 Selenium Webdriver 删除所有域上的 Cookie。
我们可以用 Selenium 删除所有域上的 Cookie。deleteAllCookies 方法用于从当前域删除所有 Cookie。首先,我们要添加 Cookie,然后获取它们,最后删除所有 Cookie。
语法
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 DeleteCookies{ 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://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 ck = driver.manage().getCookies(); System.out.println("Cookie count: "+ck.size()); // delete cookies driver.manage().deleteAllCookies(); // obtain the cookies after delete Set ch = driver.manage().getCookies(); System.out.println("Cookie count after delete: "+ch.size()); } }
输出
广告