Automation Using Selenium Webdriver

Friday 25 November 2016

How to Check All the Checkboxes Present in a Page Using Selenium

This post tells you how to check all the checkboxes present in the page.
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;


public class testng_3 {
@Test
public void checkbox() throws Exception
{
FirefoxDriver fd= new FirefoxDriver();
fd.manage().window().maximize();
fd.get(“http://www.flipkart.com/laptops/lenovo~brand/pr?sid=6bo,b5g&otracker=hp_nmenu_sub_electronics_0_Lenovo”);
Thread.sleep(10000);
List checkBoxes = fd.findElements(By.xpath(“//input[@type=’Checkbox’]”));
System.out.println(checkBoxes.size());
for(int i=0; i<checkBoxes.size(); i++){
System.out.println(checkBoxes.get(i).getText());
checkBoxes.get(i).click();
}
fd.close();
}
}

Java Program to find Max occurred character in a string



  • How to print duplicate characters from string in java?
  • Java program to count number of repeated characters in a string.
  • Java program for printing a repetitive letters in a string
  • count occurrences of each character in string java

  1. package com.javatutorial;
  2.  
  3. public class MaxOccuranceOfChar{
  4.    
  5. public static String MaxOccuredChar(String str) {
  6.  
  7.         char[] array = str.toCharArray();
  8.         int maxCount = 1;
  9.         char maxChar = array[0];
  10.  
  11.   for(int i = 0, j = 0; i < str.length() - 1; i = j){
  12.        int count = 1;
  13.    while (++j < str.length() && array[i] == array[j]) {
  14.           count++;
  15.         }
  16.  
  17.   if (count > maxCount) {
  18.      maxCount = count;
  19.      maxChar = array[i];
  20.  }
  21.  
  22.   }
  23.  
  24.         return (maxChar + " = " + maxCount);
  25.  }

  26.  public static void main(String[] args) {
  27.   
  28.   String str1=MaxOccuredChar("instanceofjava");
  29.   System.out.println(str1);
  30.  
  31.   String str2=MaxOccuredChar("aaaabbbccc");
  32.   System.out.println(str2);
  33.  
  34.   String str3=MaxOccuredChar("ssssiiinnn");
  35.   System.out.println(str3);
  36.  
  37.   String str4=MaxOccuredChar("jaaaavvvvvvvvaaaaaaaaaa");
  38.   System.out.println(str4);
  39.  
  40. }

  41. }


Output:
  1. i = 1
  2. a = 4
  3. s = 4
  4. a = 10