Selenium 如何在Katalon Studio中获取元素的所有子元素

Selenium 如何在Katalon Studio中获取元素的所有子元素,selenium,katalon-studio,Selenium,Katalon Studio,我想查找根div元素中的所有元素 这就是我正在做的: @Keyword public boolean verifySlotsOrder(int numOfSlots){ WebDriver driver = DriverFactory.getWebDriver() WebElement slotsGrid = driver.findElement(By.id('js-' + numOfSlots + '-grid')) List<WebElement> children = slo

我想查找根div元素中的所有元素

这就是我正在做的:

@Keyword
public boolean verifySlotsOrder(int numOfSlots){
WebDriver driver = DriverFactory.getWebDriver()
WebElement slotsGrid =  driver.findElement(By.id('js-' + numOfSlots + '-grid'))
List<WebElement> children = slotsGrid.findElements(By.xpath('//*[@id="js-' + numOfSlots + '-slot"]'))
for(int i = 0; i < children.size; i++){
    KeywordUtil.logInfo(children[i].getText())
    }
 //return true or false once the slots order is validated
}
这段代码的问题是,该方法无法像我所设想的那样工作。我认为xpath将匹配每个子WebElement的xpath的一部分,因此它将被添加到我的列表中。根div中元素的My id序列为:

js NUMOF插槽


其中1这里是获取所有槽div的xpath

CSS:

xpath:

//div[contains(@id,'js-8-slot') and parent::div[@id='js-8-grid']]
实施:

@Keyword
public boolean verifySlotsOrder(int numOfSlots){
    WebDriver driver = DriverFactory.getWebDriver()
    # don't need the grid here, we can access the slots directly
    List<WebElement> children = slotsGrid.findElements(By.xpath("//div[contains(@id,'js-" +Integer.toString(numOfSlots) +"-slot') and parent::div[@id='js-" +Integer.toString(numOfSlots) +"-grid']]"))

    for(int i = 0; i < children.size; i++){
        KeywordUtil.logInfo(children[i].getText())
        }
}

一种更简单的方法是使用*,它给出上下文节点的所有直接子元素

实施:

@Keyword
public boolean verifySlotsOrder(int numOfSlots){
    WebDriver driver = DriverFactory.getWebDriver()
    # don't need the grid here, we can access the slots directly
    List<WebElement> children = slotsGrid.findElements(By.xpath("//div[contains(@id,'js-" +Integer.toString(numOfSlots) +"-slot') and parent::div[@id='js-" +Integer.toString(numOfSlots) +"-grid']]"))

    for(int i = 0; i < children.size; i++){
        KeywordUtil.logInfo(children[i].getText())
        }
}

你可以分享网格和插槽的html以及你收到的任何错误吗。至少为网格和slots@supputuri我添加了HTMLim,得到以下错误:字符串'//div[@id=js-8-grid']//div[contains@id,'js-8-slot']不是有效的XPath表达式。我的错,更新了答案。我必须在中使用wratp xpath,但我在中使用了它,这就是为什么会出现错误。此外,由于您删除了slotsGrid,我想您应该使用driver.findElements,而不是slotsGrid.findElements,因为slotsGrid不再存在。这仍然为我提供了网格中ID中某处具有“js-8-slot”的所有元素,我想要的只是第一层的子元素,这是直接包含在ok中的8个div元素,我想在第一层中有同名的div。让我更新答案。
@Keyword
public boolean verifySlotsOrder(int numOfSlots){
    WebDriver driver = DriverFactory.getWebDriver()
    # don't need the grid here, we can access the slots directly
    List<WebElement> children = slotsGrid.findElements(By.xpath("//div[contains(@id,'js-" +Integer.toString(numOfSlots) +"-slot') and parent::div[@id='js-" +Integer.toString(numOfSlots) +"-grid']]"))

    for(int i = 0; i < children.size; i++){
        KeywordUtil.logInfo(children[i].getText())
        }
}
WebDriver driver = DriverFactory.getWebDriver()
//get the parent element
WebElement slotsGrid =  driver.findElement(By.id('js-' + numOfSlots + '-grid'))
//use the parent element to get all the immediate children
List<WebElement> children = slotsGrid.findElements(By.xpath("*"))