How to detect if a WebElement is present

Hello!

My objective is to detect if a WebElement is present.

So I wrote this code

List<WebElement> rows = CustomKeywords.'Table.getRowsOfATableById'(driver, 'A150')
for(int i = 1; i < rows .size(); i++) {
    def exists = WebUI.verifyElementPresent(rows.get(i).findElement(By.xpath('//td/a')), 10)
    if(exists) {
        // exists
    }
    else {
        // not exists
    }
}

However this error occured

No signature of method: static verifyElementPresent() is applicable for argument types: (org.openqa.selenium.remote.RemoteWebElement, java.lang.Integer) values: [[[[[[[CChromeDriver: chrome on XP (a0b2b2416e09d1104b3459f96697ca64)] → id: A150]] → tag name: tr]] → xpath: //td/a], …]

I don’t know how to convert CChromeDriver in RemoteWebElement. Can you help me to fix it please?

It happened because I use packages org.openqa.selenium?

Thank’s a lot!

the parameter of verifyElementPresent should be testObject from Object repository - define one using your x-path and then use findTestObject() function
in your case you will defined parametrized xpath like: //tr[${RowNumber}]//td/a
and in findTestObject - findTestObject(‘Object Repository/path_to_object’, [(‘RowNumber’) : cycle_var])

I already tried to create a TestObject and add a property.

TestObject to = new TestObject();
to.addProperty("xpath", , ConditionType.EQUALS, ?????)

But I don’t know how to build the XPath Expression based on rows which are recovered with the loop.

Hi Nicolas,

you are trying to pass RemoteWebElement as a parameter to verifyElementPresent, which expects TestObject instead. You need to convert RemoteWebElement to TestObject. Or - and it looks like better solution - use isDisplayed() method from WebElement class.

for(WebElement elem : webElemList) {
    boolean exists = elem.isDisplayed()
    if(exists) {        // exists
    }
    else {
        // not exists
    }
}
1 Like

my guess is that you are trying to get table based on ID (A150), then all it’s rows and then check if row have TD with tag
my xpath will be looking like : //table[@id=${TableID}]//tr[${RowNumber}]//td/a
i would go with something like this (more Katalon solution):

int rowCount = driver.findElements(By.xpath(’//table[@id=‘A150’]//tr’)).size()

for (def i : (1…rowCount)) {

WebUI.verifyElementPresent(findTestObject(‘Object Repository/path_to_obj’, [(‘TableID’) : ‘A150’,(‘RowNumber’) : i]))

}

P.S.: i’m writing this from memory so errors can be found in above

Hi @Marek Žitný , first thanks for your answer.

Every elements of webElemList exists.

Here an html example.

<table id="A150">
    <tr>
        <td> some text </td>
    </tr>
    <tr>
        <td> <a id='5' href='...'>Link</a>
    </tr>
    <tr>
        <td> some text </td>
    </tr>
</table>

With this example, webElemList contains three elements (

tags). But I want to know if for each row ( tag) a tag with id “5” exists or not.

@Andrej Podhajský thank’s!

I will try this solution.