java - Web 元素 Groovy isSelected() 和 selected 得到错误的结果

标签 java selenium-webdriver groovy katalon-studio

场景 我想收集表中所有选定的[复选框-选中]记录。下面是我的方法,

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
            }           
        }
    }

将表名作为参数传递,并收集表中所有选定的记录。但不幸的是总是 isCheckedisChecked1 返回 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/

相关文章:

python - 如何使用 Selenium WebDriver 和 Python 提取元素中的文本?

java - 获取 Grails 中最受欢迎的项目

shell - Groovy Init 中的 Jenkins 环境变量

java - eclipse RCP : Stopping a thread/job that freezes

java - 如何在 android studio 中安排特定日期和时间的通知?

python - 按值查找元素 Selenium/Python

firefox - Webdriver:在 Firefox 中单击 "Share Location"按钮

grails - 我如何触发 g :formRemote? 的 onFailure 事件

java - Adobe CQ5 - 从外部数据库填充下拉列表

java - Spring quartz : Job does not fire