使用Selenium WebDriver Java绑定清除浏览器Cookies

2023年 8月 28日 34.0k 0

我们可以在Selenium中清除浏览器的cookie。方法deleteCookieNamed将删除具有特定名称的cookie。所需删除的cookie名称作为参数传递给该方法。首先,我们将添加一个cookie,然后获取它,最后删除它。

语法

driver.manage().deleteCookieNamed("foo");

登录后复制

另一种方法称为deleteAllCookies删除现有域中的所有cookie。首先,我们将添加cookie,然后获取并删除它们。

Syntax

driver.manage().deleteAllCookies();

登录后复制

Example

的中文翻译为:

示例

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:Usersghs6korDesktopJavachromedriver.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 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());
}
}

登录后复制

Example

的中文翻译为:

示例

Code Implementation with 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:Usersghs6korDesktopJavachromedriver.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 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());
}
}

登录后复制

输出

使用Selenium WebDriver Java绑定清除浏览器Cookies

以上就是使用Selenium WebDriver Java绑定清除浏览器Cookies的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论