简体   繁体   中英

How to get all the values using Xpath Selenium

I want to get all the values of td[3]. I have the below code

<tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
</tr>
<tr>
    <td>4</td>
    <td>5</td>
    <td>6</td>
</tr>
<tr>
    <td>7</td>
    <td>8</td>
    <td>9</td>
</tr>
<tr>
    <td>10</td>
    <td>11</td>
    <td>12</td>
</tr>
WebElement test;
test=driver.findElement(By.xpath("./" +value)); // here /*/*/td[5] is passed as param 
System.out.println("Values : " +test.getText());

I tried /*/*/td[3] but it just gives me only the first value.

Thanks!

//tr/td[3]

返回每个第三个td元素, tr元素的子元素。

You can get all data belonging to column 3 using following code:

 List <WebElement> allRows = driver.findElements(By.xpath("\\table\\tr"));

 for(int i=0; i<allRows.size(); i++){

      WebElement col3Element = allRows.get(i).findElement(By.xpath("\td[3]"));

      System.out.println(" Row " + i "Columnn 3 data: " + col3Element.getText());

 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM