Rescue the text of an input with Java Selenium

1

Good afternoon community, I'm trying to rescue the text of the following element:

<table class="GroupsTable">
  <tbody>
    <tr>
      <td>
        <input type="checkbox" name="chk" value="1">
        " texto no seleccionado"

        <br>
        <input type="checkbox" name="chk" value="5" checked>
        " texto seleccionado"

        <br>
        <input type="checkbox" name="chk" value="3">
        " texto no seleccionado"

        <br>
        <input type="checkbox" name="chk" value="21" checked>
        " texto seleccionado"

        <br>
        <input type="checkbox" name="chk" value="12">
        " texto no seleccionado"

        <br>
      <td>
    </tr>
  </tbody>
</table>

The problem is that the text itself is outside the element and I can not capture it

Try as follows:

String include = driver.findElement(By.xpath("(//table[@class='grid contractGroupsTable']/tbody/tr[2]/td[3]/input[@checked])[1]")).getText()

This returns an empty text to me.

Any help or suggestion is welcome, thank you very much !!

    
asked by Raúl Mario Pozo Henríquez 29.11.2018 в 16:11
source

2 answers

1

I would try to get the text from the father, from the td. As follows:

String include = driver.findElement(By.xpath(".//input[@name='chk' and @checked]/ancestor::td")).getText();

You will probably have to delete spaces and line breaks.

include = include.trim();

If you want to get all you will have to use the function findElements

    
answered by 29.11.2018 / 17:13
source
0

first of all I want to thank everyone for their answers

They gave me the clues that required

The solution was finally the following:

def captureText(){
    WebDriver driver = DriverFactory.getWebDriver()
    WebElement includeText = driver.findElement(By.xpath("//table[@class='grid contractGroupsTable']/tbody/tr[2]/td[3]/input[@checked]/ancestor::td[1]"))
    List<String> includes = includeText.getText().split('\n')
    WebElement selectedInclude
    List<String> selectedIncludes = new ArrayList<>()
    for(int indexInclude = 1; indexInclude <= includes.size(); indexInclude++){
        selectedInclude = driver.findElement(By.xpath("(//input[@name='contract_groupids'])[" + indexInclude + "]"))
        if(selectedInclude.getAttribute("checked")){
            selectedIncludes.add(includes[indexInclude - 1])
        }
    }
}
    
answered by 29.11.2018 в 18:51