Is it possible to limit the scope of XPath search in Katalon?
For example, instead of searching the whole DOM it should only search a particular segment.
In my case, there are a total of 70 elements with an Xpath in the whole DOM. There are multiple div in there and I want to restrict my search to a particular div so that I can get 5 elements only.
Yes you should be able to. You write a pathway that leads from a parent tag down to the 5 child elements. You should review using DevTools on your browser.
As an example:
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import org.openqa.selenium.By as By
import org.openqa.selenium.WebDriver as WebDriver
import org.openqa.selenium.WebElement as WebElement
WebDriver driver = DriverFactory.getWebDriver();
'get my list of items'
List<WebElement> rows_table = driver.findElements(By.xpath('id("possibleItems")/div[@class="..."]'));
int rows_count = rows_table.size();
'cycle through the list and ...'
for (irow = 0; irow < rows_count; irow++)
{
Here is another example:
xml - XPath select all elements between two specific elements - Stack Overflow
No. XPath evaluation engines (= web browser) apply a given XPath expression against the whole DOM always.
You can write an XPath expression that selects 5 div elements out of 70 divs in the whole DOM.
@grylion54 gave us an conceptual example of XPath expression that tries to select some div elements out of the whole DOM.
If you haven’t studied XPath, you need to study XPath first. Have a look at some tutorials, e.g.,
You have to write the expression manually. Katalon’s Spy and Recorders are not helpful enough for you if you want to select 5 out of 70 divs; these tools are not designed to generate this sort of XPath expressions.
This is splicing the semantics, I think.
@puneettester01 You can view each “step” in your xpath as “reducing the scope” since each step narrows the search area. So, in that sense, yes you can limit the scope within this narrower, nuanced definition, like @grylion54 said:
Reworded to cover your case:
You can write an xpath rule that leads to a chosen parent element (a div
say) which leads to the (5) child elements of interest.