我想等到特定视图消失。一般来说,我可以使用以下代码来做到这一点:
val wait = WebDriverWait(driver, Duration.ofSeconds(3)) // where driver is e.g. WebDriver
val condition = ExpectedConditions.invisibilityOfElementLocated(
By.id("my.app:id/someId") // or another locator, e.g. XPath
)
wait.until(condition)
但是,这种方式不是很精确。
想象一个场景,有两个不同的视图匹配相同的谓词(定位器)。在下面的示例中,我使用了“按 ID”定位器,但它也可以是其他任何东西。
在下图中,有 3 个视图:
- 查看与我的谓词匹配的“A”(“按 ID”)
- 包含视图“C”的视图“B”
- 查看与我的谓词匹配的“C”(“按 ID”)
当我只想找到视图“C”时,例如为了点击它,我可以这样做:
driver.findElement(By.id("anotherId")).findElement("someId").click()
所以当我知道它包含视图“C”时,我可以通过首先搜索视图“B”来缩小对视图“C”的搜索范围。
这是可能的,因为方法WebElement
返回的findElement
实现SearchContext
接口,就像WebDriver
确实一样。因此,我可以选择是要在整个屏幕上搜索还是在特定的WebElement
.
在等待视图消失的情况下如何缩小搜索范围?
理想情况下,我希望是这样的:
ExpectedConditions.invisibilityOfElementLocated(
searchContext, // either a driver or WebElement
By.id(...) // the locator
)
但我还没有找到类似的东西。