-1

I have a Website https://www.ultimateqa.com/complicated-page/ on the website there is a form to fill in that has a Captcha which produces random numbers and asked the user to do the addition and enter a number.

I am trying to do this on Selenium using Java.

How can I verify the random numbers, take themr, do the calculation and then add the answer to the input field?

eugene.polschikov
  • 7,254
  • 2
  • 31
  • 44
awalker23
  • 45
  • 1
  • 6
  • Use pattern to verify one digit or 2 digit number? `Pattern digitPattern = Pattern.compile("\\d{1}");` or `Pattern digitPattern = Pattern.compile("\\d{2}");` – KunduK May 22 '19 at 11:18
  • Get the string 'X + Y = ', using split and/or replace you can extract X and Y, finally convert those to integers. – pburgr May 22 '19 at 13:26
  • @pbugr How would I do this, I am obviously pretty new to this....I can find the field which is et_pb_contact_captcha_question, but how to extract the X + Y is a little bit to much for me. – awalker23 May 22 '19 at 14:28
  • This is what I tried earlier **public void Capta() {** **WebElement X = driver.findElement(By.xpath("//*[@id=\"et_pb_contact_form_0\"]/div[2]/form/div/div/p/span"));** **System.out.println("Calculation print out" + X);** What I am trying to do is to locate the numbers and pass the value to 'X' I am then going to split them, the issue I have is that I am using the BY.Xpath, I think this locating the element but not the value in the element. – awalker23 May 22 '19 at 19:49

1 Answers1

1
  1. Locate web element holding the captcha challenge

    WebElement element = driver.findElement(By.className("et_pb_contact_captcha_question"));
    
  2. Get its text into a String:

    String captchaText = element.getText();
    
  3. Evaluate the string value using i.e. ScriptEngine

    String captchaResult = new javax.script.ScriptEngineManager().getEngineByName("javascript").eval(captchaText).toString();
    
  4. That should be it:

    enter image description here

In more complex captcha challenges where you cannot find the values in the page source you will have to go for an OCR or image recognition library like OpenCV or Tesseract or SeeTest

Dmitri T
  • 159,985
  • 5
  • 83
  • 133