Search code examples
seleniumpageobjectspage-factory

trying to handle webtable in POM using pagefactory but error is thrown


strong textI was trying to handle webtable in POM using pagefactory but its throwing error

if i try nto paste xpath directly but otherwise its not working

 @FindBy (xpath = "//table[@id='userTable']/tbody/tr[")
     WebElement before_xpath;

     @FindBy (xpath = "]/td[2]")
     WebElement after_xpath;



     @FindBy (xpath = "//table[@id='userTable']/tbody/tr")
     List<WebElement> namelist;



     //Intialising PageObjects

     public users_page() {

         PageFactory.initElements(driver, this);


     }


     //Actions


  public void userlist(String nm, String un, String pw, String cpw, String hub) {


        List<WebElement> row =namelist;
           int row_count = row.size();
         System.out.println("Total no of rows " +row_count);

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


             WebElement actual_xpath = before_xpath +i +actual_xpath;
             System.out.println("Total  " +actual_xpath);

  }

  }}

It is throwing error on WebElement actual_xpath = before_xpath +i +actual_xpath;.

its showing The operator + is undefined for the argument type(s) WebElement, int

so hw i can handle this


Solution

  • WebElement do not handle, Concatenation of WebElement itself. Here before_xpath, after_xpath are WebElement itself.

    If you want to concate something as what you are looking for (WebElement actual_xpath = before_xpath +i +after_xpath)

    There should be string datatype of before_xpath, i and after_xpath.

    So your WebElement actual_xpath, will be have correct String to locate Xpath.

    Also, concatenation of String should be in correct format of Xpath.