Hello, dear friends!

At the time of writing autotests, there was a need to transfer not a static xPath, but a more or less dynamic one. I will explain with an example:

There is xPath:

"//select[@id = '1']/option[@text() = 'Текст']" 

There are variables:

 String num = "1"; String text = "Текст"; 

You must pass these variables inside xPath:

 "//select[@id = num]/option[@text() = text]" 

Please tell me how to do this? Thank!

    1 answer 1

    Instead of a constant, form the xPath string each time:

     String xpath = "//select[@id = '" + num + "']/option[@text() = '" + text + "']"; 

    or so

     new StringBuilder("//select[@id = '") .append(num) .append("']/option[@text() = '") .append(text) .append("']").toString(); 

    or using String#format() :

     String.format("//select[@id = '%s']/option[@text() = '%s']", num, text); 

    There are lots of options.