java - 如何在 Selenium webdriver 中获取对象的通用 xpath
问题描述
String str = "some value" ;
getTestBase().getDriver().findElement(By.xpath("//h2[.='" + str + "']//parent::div//div[@id='sectionList'][1]/section/div/button")).click();
我需要将上述内容放入循环中,以便 [1] 每次都不断增加,如何更新上述整数的 xpath ?
解决方案
String str = "some value" ;
WebDriver driver = getTestBase().getDriver();
for(int i=0; i<5; i++){
driver.findElement(By.xpath("//h2[.='" + str
+"']//parent::div//div[@id='sectionList']["+i+"]/section/div/button")).click();
}
这应该可以解决您的问题。5 只是我为编写解决方案假设的任意限制。
另一方面,您可以尝试获取所有元素并将元素存储在列表中。然后解析列表以获取按钮并单击它们。
就像是:
String str = "some value" ;
WebDriver driver = getTestBase().getDriver();
List<WebElement> listOfButtons = driver.findElements(By.xpath("//h2[.='" + str + "']//parent::div//div[@id='sectionList']//section/div/button""));
for(WebElement el: listOfButtons){
el.click();
}
推荐阅读
- xamarin - 我的应用程序将数据发送到 COSMOS 数据库。我需要在 Android Manifest 中启用 Internet 权限吗?
- reactjs - 使用 React Map GL 渲染超过 1000 个标记
- reactjs - componentDidmount() 中的反应状态没有改变
- amazon-neptune - 无法使用 btoto3 创建海王星集群
- elasticsearch - VirtualBox 上的弹性服务器空间不足?
- php - 如何为 Eloquent 设置现有连接
- regex - VBA Excel中正则表达式中的文本替换
- java - 根据当前请求 URI 获取用户的权限
- android - DatePickerDialog 月份显示为数字(Jan 为 1)
- flutter - 无法使用 firebase_messaging 在 iOS 上的 Flutter 应用中订阅主题