javaseleniumgroovykeywordkatalon

How to dynamically find web element by XPath in Selenium using Katalon


driver.findElement(By.xpath("//*[contains(text(),'"+ ProjectName +"')]")).click();

I tried to use this code to find web element dynamically, but when I tried to call it on my test case seems that it cannot have an input value since this code is a part of my custom keyword code. Can someone help me try to find a way on how to do it. Thank you very much!


Solution

  • To click on element you can use following method:

    public void clickOnElement(String xpath) {
        driver.findElement(By.xpath(xpath)).click();
    }
    

    To format a String according to passed parameter you can use this:

    public String stringFormat(String template, String parameter){
        return String.format(template,parameter);
    }
    

    For the specific use like in your question the template can be defined like this:

    String elementTextTmplt = "//*[contains(text(),'%s')]"