Page 43: How to solve math captcha in selenium webdriver using java?.

How to solve math captcha in selenium webdriver using java?.

  1. public class Mathcaptcha {  
  2.   
  3.     public static void main(String[] a) throws InterruptedException {  
  4.         // Initialize Web driver      
  5.         WebDriver driver = new FirefoxDriver();  
  6.         //Maximize browser window     
  7.         driver.manage().window().maximize();  
  8.         //Go to Page      
  9.         driver.get("http://localhost/mathquestion/signin");  
  10.   
  11.         // Type username  
  12.         WebElement username = driver.findElement(By.id("edit-name"));  
  13.         username.clear();  
  14.         username.sendKeys("username");  
  15.   
  16.         //Type Password  
  17.         WebElement password = driver.findElement(By.id("edit-pass"));  
  18.         password.clear();  
  19.         password.sendKeys("aaaaaa");  
  20.   
  21.         // get Math question  
  22. String mathquestionvalue = driver.findElement(By.xpath(".//*[@id='user-login']/div/fieldset/div/div[2]/span")).getText().trim();  
  23.   
  24.         // remove space if exist  
  25.         String removespace = mathquestionvalue.replaceAll("\\s+""");  
  26.         // get two numbers   
  27.         String[] parts = removespace.split("\\+");  
  28.         String part1 = parts[0];  
  29.         String part2 = parts[1];  
  30.         String[] parts1 = part2.split("\\=");  
  31.         String part11 = parts1[0];  
  32.   
  33.         // sum two numbers  
  34. int summation = Integer.parseInt(part1) + Integer.parseInt(part11);  
  35.   
  36.         // Math Capcha value  
  37.         WebElement capta = driver.findElement(By.xpath(".//*[@id='edit-captcha-response']"));  
  38.         capta.clear();  
  39.         capta.sendKeys("" + summation);  
  40.   
  41.         //Click on SignIn button  
  42.         driver.findElement(By.id("edit-submit")).click();  
  43.   
  44.         //close browser  
  45.         driver.quit();  
  46.     }  
  47. }  

About Me

My photo
Pune, Maharastra, India
You can reach me out at : jimmiamrit@gmail.com