遍历列表并单击元素 (Cucumber/Java)

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

我在一个页面上有 20 个按钮,每个按钮都有相似的 ID id='gvFobs_DXSelBtn**',其中 ** 代表按钮的位置编号,所以 id='gvFobs_DXSelBtn01' 代表位置一个和 id='gvFobs_DXSelBtn20' 用于位置 20。 我不想为每个位置创建一个 WebElement。我正在尝试找到一种方法来列出它们遍历每个位置,直到我到达所需的位置然后单击。

有谁知道我该如何解决这个问题?

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

您仍然需要为每个按钮创建 WebElement 个实例。获取 WebElement 个对象的列表不是问题。问题是调用 click() 后会发生什么。根据 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.

对于此组件以及在页面重新加载之前获得的任何其他 WebElement 引用都是如此。这是因为在点击事件之后,整个 DOM 都失效了,所有 WebElement 引用在那个时候都被认为是陈旧的(旧的)。

这对你意味着什么?这意味着,如果这些按钮中的任何一个触发了页面重新加载,则此方法将不起作用。这也意味着即使此解决方案适用于这种特定情况,也不能保证它适用于所有情况,您应该明智地使用这种方法。

强烈反对。最安全的方法是有步骤独立点击每个按钮。