简体   繁体   中英

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**' where the ** represents the position number of the button so id='gvFobs_DXSelBtn01' for position one and id='gvFobs_DXSelBtn20' for position 20. I don't want to create a WebElement for each position. 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?

I have 20 buttons on a single page each one of these buttons has the similar ID...I don't 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM