如何在 Katalon Studio 中获取所有 children 元素
How to get all children of element in 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 = 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 将匹配每个 child WebElement 的 xpath 的一部分,因此它将被添加到我的列表中。我的根 div 中元素的 id 序列是:
js-numOfSlots-slot<slotNum>
其中 1 <= slotNum <= numOfSlots
如何正确生成遵循此 ID 序列并具有公共 parent WebElement 的 WebElement 列表。这也可以用 Katalon TestObjects 实现吗?
这里是HTML。注意,我只想要直系children,而不是所有后代。
更新:
通过这样做,我可以得到我根 div 的所有后代:
List<WebElement> children = slotsGrid.findElements(By.xpath("//*[contains(@id, 'js-8-slot')]"))
如果我在我的 xpath 末尾添加 /div/div/div
,我会得到某些 child 元素,但我仍然不确定如何正确地只得到第一层 child元素(立即 children)。
这是获取所有插槽 div 的 xpath。
CSS:
div[id^='js-8-grid'] >div[id^='js-8-slot']
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())
}
}
一种更简单的方法是使用 ("*")
,它给出上下文节点的所有直接子元素。
实施:
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("*"))
我想找到根 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 将匹配每个 child WebElement 的 xpath 的一部分,因此它将被添加到我的列表中。我的根 div 中元素的 id 序列是:
js-numOfSlots-slot<slotNum>
其中 1 <= slotNum <= numOfSlots
如何正确生成遵循此 ID 序列并具有公共 parent WebElement 的 WebElement 列表。这也可以用 Katalon TestObjects 实现吗?
这里是HTML。注意,我只想要直系children,而不是所有后代。
更新:
通过这样做,我可以得到我根 div 的所有后代:
List<WebElement> children = slotsGrid.findElements(By.xpath("//*[contains(@id, 'js-8-slot')]"))
如果我在我的 xpath 末尾添加 /div/div/div
,我会得到某些 child 元素,但我仍然不确定如何正确地只得到第一层 child元素(立即 children)。
这是获取所有插槽 div 的 xpath。
CSS:
div[id^='js-8-grid'] >div[id^='js-8-slot']
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())
}
}
一种更简单的方法是使用 ("*")
,它给出上下文节点的所有直接子元素。
实施:
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("*"))