如果有多个元素具有相同的 xpath,"wait.until(ExpectedConditions.elementToBeClickable(By.xpath()))" 会做什么?

what does "wait.until(ExpectedConditions.elementToBeClickable(By.xpath()))" do if there are more than one element with the same xpath?

假设我使用:

WebElement we = wait.until(ExpectedConditions.elementToBeClickable(By.xpath()));

获取一个网络元素,但是如果有多个元素具有相同的 xpath 但其中只有一个应该是可点击的(可见的)怎么办?它会给我带来那个确切的网络元素吗?或者它会因为那些不可见的而抛出异常吗?

如果您查看 implementation of ExpectedConditions.elementToBeClickable you'll see that it gets its element through ExpectedConditions.visibilityOfElementLocated. In turn this function uses a findElement function to get its element, which in turn calls WebDriver.findElement. The the documentation for WebDriver.findElement 声明它 returns 匹配 select 或

的第一个元素

因此,即使您有多个元素与传递给 ExpectedConditions.elementToBeClickable 的内容相匹配,测试也只会考虑第一个元素。如果第一个元素可以被点击,那么条件就满足了。如果无法单击第一个元素,则等待将超时。是否可以单击其他匹配元素并不重要。您应该仅将 selector 调整为 select 您感兴趣的元素。