how to write xpath with concat function for variable

Viewed 88

Actual String in excel is=> "J" String First Part''SecondPart

String Itemcodefromexcel=sheet.getRow(1).getCell(1).getStringCellVal

String Itemcode=replace_it(Itemcodefromexcel);

driver.findElement(By.xpath("//td[text()=concat("+Itemcode+")").getText();

above statement not working

but if i manually copy String from above method(replace_it) then its working ex : driver.findElement(By.xpath("//td[text()=concat(\'\"\',\'J\"\',\" String First Part\'\",\"\'\",\"SecondPart\")").getText();

public String replace_it(String _str){
       List<String> str_list = new ArrayList<String>();
       String _str1 = "";
       
       for (int i = 0; i < _str.length(); i++){
           char char_c = _str.charAt(i);
           char char_a = '\'';
           char char_b = '"';
           int int_a = (int)char_a;
           int int_b = (int)char_b;
           int int_c = (int)char_c;
           
           if(int_c == int_a){
               str_list.add( "\\\""  + _str1 + "\\\'\\\"");
               _str1 = "";
           }
           else if(int_c == int_b){
               str_list.add( "\\\'"  + _str1 + "\\\"\\\'");
               _str1 = "";
           }
           else{
               _str1 += char_c;
           }
       }
       str_list.add("\\\"" +_str1 + "\\\"");
       return String.join(",",str_list);
   }

replace_it method will convert string="J" String First Part''SecondPart to string=\'\"\',\'J\"\',\" String First Part\'\",\"\'\",\"SecondPart\"

so my question is if i copy manually string from same method then its working but via variable its not working so i am guessing issue is in encoding xpath concat but couldn't resolved it

language : selenium with java

also tried below statement but not working

driver.findElement(By.xpath("//td[text()=concat(\'"+Itemcode+"\')").getText();

driver.findElement(By.xpath("//td[text()=concat('"+Itemcode+"')").getText();

Note : ItemCode is dynamic variable ,whose value come from excel

0 Answers
Related