Saturday, July 27, 2024

Java code to automate the extraction of two numbers from a web page

 package system;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;

public class Practice {

   WebDriver driver;

@BeforeMethod
public void setUp() {
  WebDriverManager.chromedriver().setup();
  driver = new ChromeDriver();
}

@Test(description = "extraction of two numbers from a web page")
public void test() throws InterruptedException {

driver.get("https://ultimateqa.com/complicated-page");

// Locate the element containing the CAPTCHA question
WebElement captchaElement = driver.findElement(By.className("et_pb_contact_captcha_question"));

// Extract the CAPTCHA question text
String captchaText = captchaElement.getText().trim(); // Example: "5 + 6"

// Split the text to get the operands
String[] parts = captchaText.split("\\+");
int firstNumber = Integer.parseInt(parts[0].trim());
int secondNumber = Integer.parseInt(parts[1].trim());

// Calculate the sum
int sum = firstNumber + secondNumber;

// Print the sum (for debugging purposes)
System.out.println("Extracted Numbers: " + firstNumber + " + " + secondNumber + " = " + sum);

// Enter the sum into the CAPTCHA input field
WebElement captchaInputField = driver.findElement(By.className("et_pb_contact_captcha"));
captchaInputField.sendKeys(String.valueOf(sum));
}
}

No comments:

Post a Comment