How does WebDriver locate and interact with a specific element on a web page?

197    Asked by ColinPayne in QA Testing , Asked on Jun 14, 2024

 There is a scenario where I am trying to test a web-based application that involves filling out a multi-step form. How can I use the WebDriver to ensure that each step of the form is completed correctly before proceeding to the next step? 

Answered by Connor Peake

In the context of selenium, here are the steps given:-

Locating the element

You can use the various locators such as ID, name, class name, CSS selector, Xpath, etc to identify the element uniquely on the web-based page.

Interacting with the elements

Once the element is located then you can act such as clicking, typing text, selecting options, etc by using the WebDriver methods.

Verifying the element properties

You can also verify the various properties of the element like text, attribute, visibility so on by using the technique or the method called WebDriver.

By following these steps you can easily Interact with the specific element on a web-based page by using the WebDriver.

Here is a coding structure given which would demonstrate how you can locate band also interact with a specific element on a web-based page by using the WebDriver:-

Import org.openqa.selenium.By;
Import org.openqa.selenium.WebDriver;
Import org.openqa.selenium.WebElement;
Import org.openqa.selenium.support.ui.ExpectedConditions;
Import org.openqa.selenium.support.ui.WebDriverWait;
Import org.openqa.selenium.chrome.ChromeDriver;
Public class MultiStepFormTest {
    Public static void main(String[] args) {
        // Set up the WebDriver
        WebDriver driver = new ChromeDriver();
        // Navigate to the web application
        Driver.get(http://example.com/multi-step-form);
        // Create a WebDriverWait instance with a timeout
        WebDriverWait wait = new WebDriverWait(driver, 10);
        // Step 1: Fill out the first part of the form
        WebElement firstNameField = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(“first_name”)));
        firstNameField.sendKeys(“John”);
        WebElement lastNameField = driver.findElement(By.id(“last_name”));
        lastNameField.sendKeys(“Doe”);
        WebElement nextButtonStep1 = driver.findElement(By.id(“next_step_1”));
        nextButtonStep1.click();
        // Verify step 1 is completed and step 2 is loaded
        WebElement step2Header = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(“step_2_header”)));
        If (step2Header.isDisplayed()) {
            System.out.println(“Step 1 completed successfully and Step 2 is displayed.”);
        }
        // Step 2: Fill out the second part of the form
        WebElement emailField = driver.findElement(By.id(“email”));
        emailField.sendKeys(john.doe@example.com);
        WebElement phoneField = driver.findElement(By.id(“phone”));
        phoneField.sendKeys(“1234567890”);
        WebElement nextButtonStep2 = driver.findElement(By.id(“next_step_2”));
        nextButtonStep2.click();
        // Verify step 2 is completed and step 3 is loaded
        WebElement step3Header = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(“step_3_header”)));
        If (step3Header.isDisplayed()) {
            System.out.println(“Step 2 completed successfully and Step 3 is displayed.”);
        }
        // Step 3: Fill out the third part of the form
        WebElement addressField = driver.findElement(By.id(“address”));
        addressField.sendKeys(“123 Main St”);
        WebElement cityField = driver.findElement(By.id(“city”));
        cityField.sendKeys(“Anytown”);
        WebElement submitButton = driver.findElement(By.id(“submit_form”));
        submitButton.click();
        // Verify form submission
        WebElement confirmationMessage = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(“confirmation_message”)));
        If (confirmationMessage.isDisplayed()) {
            System.out.println(“Form submitted successfully: “ + confirmationMessage.getText());
        }
        // Close the browser
        Driver.quit();
    }
}


Your Answer

Answer (1)

WebDriver, typically used with Selenium, is a tool that automates web browser interactions. It locates and interacts with elements on a web page using various locating strategies and can simulate user actions like clicking, typing, and navigating. 

To test a multi-step form, you can use WebDriver to fill out each step and validate that the form proceeds to the next step correctly.

Locate and Fill Out Elements for Each Step:

Identify the elements for each step using appropriate locators.

Fill in the required fields and click the "Next" or "Submit" button to proceed to the next step.

Use Assertions to Validate Each Step:

After completing each step, use assertions to verify that the expected elements (like headers, inputs, or buttons) for the next step are present.

This ensures that the form has moved to the correct step before proceeding. scratch geometry dash


3 Months

Interviews

Parent Categories