Groovy 使用单引号在 xpath 中传递字符串值 - Katalon

Groovy passing string value inside the xpath with single quote - Katalon

我在 Katalon Studio 中使用 Groovy 创建了自定义关键字函数。

下面是我的函数

def referenceTabRecordSelection(String expectedRefState,String tableid){
        WebDriver driver = DriverFactory.getWebDriver()
        WebElement RefTable = driver.findElement(By.xpath('//*[@id= '+tableid+' ]/tbody'))
}

"tableid" 字符串在 XPath 中传递。

执行时我得到 NoSuchElementException

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id=usRef]/tbody"}

经过调查,xpath 中的 id 值未填充单引号,如

[@id='usRef']/tbody

请告诉我如何使用单引号在 XPath 中传递字符串变量值。

使用Groovy的字符串插值:

def referenceTabRecordSelection(String expectedRefState,String tableid){
    WebDriver driver = DriverFactory.getWebDriver()
    WebElement RefTable = driver.findElement(By.xpath("//*[@id='${tableid}']/tbody"))
}

请注意,要使字符串插值起作用,您需要使用双引号开始插值字符串。