How to find elements by partial-ID?

Hi everyone, how u can see, I have a lot of elements with the same partial IDs, how can I find all elements by partial-ID?

1 Like

You can use functions that allow you to parse part of your attribute, the <id> in your case. As an example,

(id, ConditionType.CONTAINS, 'ct100$MainPlaceHolder')

However, if there are as many as you seem to have, the <id> is supposed to be unique, so you will have to parse out that part which makes the <id> unique or go for another attribute, such as <name>. And you may even need to add more than one attribute to create an unique pathway to find the element.

Can try something like this. It will create a list of all Elements containing the partial ID.

import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement
import com.kms.katalon.core.webui.driver.DriverFactory

WebDriver driver = DriverFactory.getWebDriver()

List <WebElement> idList = driver.findElements(By.xpath("//*[contains(@id, 'ct100$MainPlaceHolder')]"))


Thanks, your soulution is the best, it works, but I I encountered another problem, that not only those elements has this part of ID, there is any solution to add the required table data or td?

You may have to have a “longer” pathway to uniquely describe your elements. As an example, you might start from an element that is “higher”, or “lower”, in the page than these elements, using “following” or “preceding” respectively. However, without us seeing your HTML, you will have to do your research about XPath on this forum and other.

There is certainly more you can add to this solution to become more specific if you have better examples of what you’re looking for.

if only the ones you want end in “_NVSTRESC1” then add and "and contains(@id, ‘_NVSTRESC1’) to the xpath.

1 Like