How to handle Cookies using Selenium Webdriver?



  • Sometimes, we face issues because of cookies session created by Selenium scripts and we have to delete them using Selenium Webdriver. So, here is the code:

public class Cookies
{
    public static void main(String[] args) 
    {
        WebDriver driver=new FirefoxDriver();
        driver.get("http://in.quikr.com/");
        
        Set cookies=driver.manage().getCookies();
        
        //To fetch out the number of cookies used by this site
        System.out.println("Number of cookies in this site "+cookies.size());
        
     //Using advanced For Loop
        for(Cookie cookie:cookies)
        {
            System.out.println(cookie.getName()+" "+cookie.getValue());
            
            //This will delete cookie By Name
            driver.manage().deleteCookieNamed(cookie.getName());
            
             }
         //This will delete all cookies.
        driver.manage().deleteAllCookies();
           } }

For any query, ContactUs

Mouse Hover in selenium



public static void main(String[] args) throws InterruptedException {

WebDriver driver = new FirefoxDriver();
driver.get("http://www.timeanddate.com/");
driver.manage().window().maximize();
WebElement menu = driver.findElement(By.xpath("//*[@id='nav']/ul/li[3]/a"));
Actions action = new Actions(driver);
action.moveToElement(menu).perform();
WebElement submenu = driver.findElement(By.xpath("//*[@id='nav']/ul/li[3]/ul/li[4]/a"));
action.moveToElement(submenu).click().perform();

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

For any queries, ContactUs

Get all links of a Webpage using Selenium


How to test links of any website?

public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in/");
List links = driver.findElements(By.tagName("a"));
System.out.println(links.size());    // to check the number of links present at webpage
for (int i = 1; i {
System.out.println(links.get(i).getText());
}
}