Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Iterate through a list and click element (Cucumber/Java)

I have 20 buttons on a single page each one of these buttons has the similar ID of id=’gvFobs_DXSelBtn**’ the two * represent the position number of the button so id=’gvFobs_DXSelBtn01′ for position one and id=’gvFobs_DXSelBtn20′ for position 20
I dont want to create a webelement for each position so I am trying to find a way to list them iterate through each position until I reach the desired position and then click.

Does anyone have any idea on how I can approach this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I have 20 buttons on a single page each one of these buttons has the similar ID…I dont [sic] want to create a webelement for each position

You will still need to create WebElement instances for each button. Obtaining a list of WebElement objects is not the problem. The problem is what happens after invoking click(). According to the WebElement API

Click this element. If this causes a new page to load, you should discard all references to this element and any further operations performed on this element will throw a StaleElementReferenceException.

This is true for this component and any other WebElement reference obtained before the page reload. This is because after the click event, the entire DOM is invalidated and all WebElement references are considered stale (old) at that time.

What does this mean to you? It means that, if any of those buttons triggers a page reload, this approach won’t work. It also mean that even if this solution works in this particular case, it is not guaranteed that it will work on every case and that you should use this approach judiciously.

I strongly recommend against it. The safest way to approach is to have steps to click each button independently.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading