22 Ağustos 2018 Çarşamba

Selenium add cookie

public void addCookie() { driver= new FirefoxDriver(); String URL="http://flipkart.com/"; driver.navigate().to(URL);                 //we should pass name and value for cookie as parameters                 // In this example we are passing, name=mycookie and value=123456789123 Cookie name = new Cookie("mycookie", "123456789123"); driver.manage().addCookie(name);                 // After adding the cookie we will check that by displaying all the cookies. Set<Cookie> cookiesList =  driver.manage().getCookies(); for(Cookie getcookies :cookiesList) {     System.out.println(getcookies ); } }


Create cookies using the Java API as follows:
Cookie ck = new Cookie("name", "value");
driver.manage().addCookie(ck);
Create cookies using the Python API as follows:
driver.add_cookie({'name': 'foo', 'value': 'bar'})


public class AddCookie {
 public static void main(String[] args) {
  // set chrome driver exe path
  System.setProperty("webdriver.chrome.driver", "C:/Users/user/Pictures/chromedriver.exe");
  WebDriver driver = new ChromeDriver();
  driver.manage().timeouts().implicitlyWait(200, TimeUnit.SECONDS);
  driver.get("http://google.com");

  // set the name and value for the cookie
  Cookie coo = new Cookie("karthiQ", "chercher.tech");

  // add the cookie
  driver.manage().addCookie(coo);

  // please donot write the code to close the browser.
 }


 Cookie coo = new Cookie("karthiQ", "chercher.tech");

  // set the name and value for the cookie
  Cookie ren = new Cookie("selenium-webdriver.com", "Renamed to chercher.tech");

  // add the cookies
  driver.manage().addCookie(coo);
  driver.manage().addCookie(ren);

  // delete the 'karthiQ' cookie
  driver.manage().deleteCookieNamed("karthiQ");

  // please donot write the code to close the browser.
 }

Hiç yorum yok:

Yorum Gönder