B123
B123

Reputation: 45

How to concatenate two webelement, iterate it and get the text out of concatenated Xpath

I am working with Page object model

My Page objects are as below:

    @FindBy(xpath = "//kendo-grid-list//table/tbody/tr/td[9]//div/p")
    List<WebElement> milestoneListSize;

    private static final String milestoneEditButton1 = "//uif-checklist-complex-group[";

    private static final String milestoneEditButton2 = "]//uif-checklist-table/table/tbody/tr/td[6]//a";

    public void clickMilestoneUser(milestoneListSize, String milestoneEditButton1 , String milestoneEditButton2 ) {

        String Actual;
        int totalsize = milestoneListSize.size();

        for (int i = 1; i <= totalsize; i++) {
        Actual = driver.findElement(By.xpath(milestoneEditButton1 + i + milestoneEditButton2)).getText();
        System.out.println(Actual );
        }

    }

Don't know why this piece of code is not working? Thanks for help in advance

Upvotes: 0

Views: 2047

Answers (2)

Riswan chatholi
Riswan chatholi

Reputation: 159

Use the first code if you want the concatenated result and second for seperate result

@FindBy(xpath = "//kendo-grid-list//table/tbody/tr/td[9]//div/p")
List<WebElement> milestoneListSize;

String milestoneEditButton1 = "//uif-checklist-complex-group[%d]//uif-checklist-table/table/tbody/tr/td[6]//a";

public void clickMilestoneUser(String milestoneEditButton1) {

        String actual = "";

        int totalsize = milestoneListSize.size();

        for (int i = 1; i <= totalsize; i++) {

            actual = actual+driver.findElement(By.xpath(String.format(milestoneEditButton1, i))).getText();
        }

        System.out.println(actual);

    }

2:

@FindBy(xpath = "//kendo-grid-list//table/tbody/tr/td[9]//div/p")
List<WebElement> milestoneListSize;

String milestoneEditButton1 = "//uif-checklist-complex-group[%d]//uif-checklist-table/table/tbody/tr/td[6]//a";

public void clickMilestoneUser(String milestoneEditButton1) {

        String actual = "";

        int totalsize = milestoneListSize.size();

        for (int i = 1; i <= totalsize; i++) {

            actual = driver.findElement(By.xpath(String.format(milestoneEditButton1, i))).getText();
            System.out.println(actual);
        }

    }

Upvotes: 0

Hrisimir Dakov
Hrisimir Dakov

Reputation: 587

There are several issues with your code, but I won't be able to cover them all.

  • You have defined milestoneEditButton(1&2) as 'private static' fields meaning that they are accessible from inside every method in this class and you don't need to pass them as method params.

  • Instead of using 2 separate parts to construct the locator, you can have a single string template:

    private static final String XPATH_EDIT_BTN = "//uif-checklist-complex-group[%d]//uif-checklist-table/table/tbody/tr/td[6]//a";
    

That can be used to construct the locator:

By.xpath(String.format(XPATH_EDIT_BTN, i))
  • The value of the 'Actual' variable gets reassigned every time 'Actual = ...' is called. You should be using 'Actual += ...' instead, but it's best to use StringBuilder:

    StringBuilder buffer = new StringBuilder();
    for (int i = 1; i <= totalsize; i++) {
        By loc = By.xpath(String.format(XPATH_EDIT_BTN, i));
        buffer.append(driver.findElement(loc).getText());
    }
    System.out.println(buffer.toString());
    

Upvotes: 1

Related Questions