0

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";
SternK
  • 11,649
  • 22
  • 32
  • 46
rupam
  • 3
  • 1

2 Answers2

0

Playwright provides a method getByTestId() to locate an element based on its data-testid attribute. You can check this section of the documentation to know more about this method.

So, can you please check whether the following code works for you?

page.getByTestId("next-button").click();
0

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.

Kepler452B
  • 130
  • 1
  • 1
  • 9