场景 我想收集表中所有选定的[复选框-选中]记录。下面是我的方法,
def getAlltheSelectedRecordId(String tableName){
WebDriver driver = DriverFactory.getWebDriver()
WebElement RefTable = driver.findElement(By.xpath("//*[@id='${tableName}']/tbody"))
List<WebElement> RowsOf_RefTable = RefTable.findElements(By.tagName('tr'))
int Totalcount = RowsOf_RefTable.size()
for (int row : (1..Totalcount)) {
boolean isChecked = driver.findElement(By.xpath("//*[@id='${tableName}']/tbody/tr[' + row + ']/td[2]/input")).isSelected()
println isChecked
boolean isChecked1 = driver.findElement(By.xpath("//*[@id='${tableName}']/tbody/tr[' + row + ']/td[2]/input")).selected
println isChecked1
if(isChecked){
String selectedRecordID = driver.findElement(By.xpath("//*[@id='${tableName}']/tbody/tr[' + row + ']/td[5]/a")).getText()
println selectedRecordID
}
}
}
将表名作为参数传递,并收集表中所有选定的记录。但不幸的是总是 isChecked
和 isChecked1
返回 false
请指导我问题出在哪里
最佳答案
问题出在字符串组合在一起的方式上,并且通过语法高亮显示,问题已经显而易见:
By.xpath("//*[@id='${tableName}']/tbody/tr[' + row + ']/td[2]/input")
"...' + row + '..."
不会将 row
的值放入字符串中 - 只有这个确切的“代码”。
解决方案是使用正确的引号来结束字符串:
By.xpath("//*[@id='${tableName}']/tbody/tr[" + row + "]/td[2]/input")
或者更好的是,坚持在字符串开头已经开始的方案:使用 Groovys 字符串替换:
By.xpath("//*[@id='${tableName}']/tbody/tr[${row}]/td[2]/input")
关于java - Web 元素 Groovy isSelected() 和 selected 得到错误的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56222950/