Found 2 web elements with id

Hello,

I am getting 2 web element with same name and id so how can i choose second element?

Thanks

This means your source HTML is invalid.

You should pass this back to the team that wrote the HTML, tell them it’s not valid to have more than one element with the same ID.

Rakesh,

I agree with Russ 100%. As stated in the HTML specification (3.2 Elements — HTML5), the id value must be unique amongst all the IDs in a HTML.

But it would worth knowing how to choose the 2nd node of a node-set identified by the same ID.

Let me assume the target HTML is as such:

<html>
<body>
<div id="node">text 1</div>
<div id="node">text 2</div>
<div id="node">text 3</div>
</body>
<html>

Then you would have a set of Test Objects:

  • Page_6949/div_text1
  • Page_6949/div_text2
  • Page_6949/div_text3

Each of these Test Objects would have selector by xpath. For example, the div_text2 should have following selector:

//div[@id="node"][2]

This xpath expression is what you need. It selects the 2nd node in the node-set identified by //div[@id=“node”]


You should check off “tag”, “id”, “text” factors as these are not necessary. “xpath” factor is enough.

Then you would want to write you test case as follows:

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
WebUI.openBrowser('')
WebUI.navigateToUrl('http://demoaut-mimic.kazurayam.com/6949_testbed.html')
WebUI.verifyElementText(findTestObject('Page_6949/div_text 1'), 'text 1')
WebUI.verifyElementText(findTestObject('Page_6949/div_text 2'), 'text 2')
WebUI.verifyElementText(findTestObject('Page_6949/div_text 3'), 'text 3')
WebUI.closeBrowser()

This test case would succeed.

how_to_identify_an_element_among_multiple_elements_with_same_id.png

1 Like