Search code examples
playwrightplaywright-testplaywright-java

Unable to click on Next button after checking the checkbox


I have a checkbox and next button below it.

private String CHECKBOX = "[data-testid='checkbox_with_label']";
private String NEXT_BTN = "//button[@data-testid=\"next-button\"]/div";

I am checking the checkbox and then clicking on the Next button, but it just checks the checkbox and the program stops. I think it is not finding the Next button.

Below is the implementation:-

@Step("Select Termination checkbox information")
    public void selectCheckBox() {
        page.locator(CHECKBOX).evaluate("CHECKBOX => CHECKBOX.checked = true");   
    }

 @Step("Click next button")
    public void next() {
        page.locator(NEXT_BTN).click();
    }

I am not sure if my Xpaths are incorrect , please let me know how can I create proper Xpaths if that's the case. The Xpath I get from browser is this - //*[@id="page-wrap"]/main/div/div/div[17]/div/div/div[2]/button

enter image description here

I have tried multiple Xpaths for Next button , but seems not one is working

 //private String NEXT_BTN = "[data-testid='next-button']";
 //private String NEXT_BTN = "next-button";   
 //*[@id="page-wrap"]/main/div/div/div[17]/div/div/div[2]/button
 //private String NEXT_BTN = "//*[@id=\"page-wrap\"]/main/div/div/div[17]/div/div/div[2]/button";

Solution

  • There is a chrome extension that you can use to get the exact xpath of an element. You can refer here Selectorsub

    I think you have not provided enough source code. It will be difficult for people to give you exact support.

    P/S: In your code, for simplicity and clarity, you can use page.locator(CHECKBOX).setChecked(true); to tick the checkbox. Click here to refer methodsetChecked.