Question

I have been struggling with deleting a row from a table. I really can't work out what I'm doing wrong, I have done very similar actions on very similar tables and not had issues.

Below is the Table layout in the HTML. The first TR is the headers (which needs to be ignored) after that each TR equals an entry, and there is a limit of 4 entries.

<table id="table" cellspacing="0" cellpadding="0" style=" border:0px black solid;WIDTH:100%;">
<tbody>
<tr>
<tr onclick="jsEvent('list item 1)">
<td class="attributes" style="font-size:12px ;font-family: sans-serif ;color: black ;background: #FFFFFF ">BIGNAME</td>
<td class="attributes" style="font-size:12px ;font-family: sans-serif ;color: black ;background: #FFFFFF "/>
<td class="attributes" style="font-size:12px ;font-family: sans-serif ;color: black ;background: #FFFFFF "/>
<td class="attributes" style="font-size:12px ;font-family: sans-serif ;color: black ;background: #FFFFFF "/>
<td class="attributes" style="font-size:12px ;font-family: sans-serif ;color: black ;background: #FFFFFF "/>
<td class="attributes" style="font-size:12px ;font-family: sans-serif ;color: black ;background: #FFFFFF "/>
<td class="attributes" style="font-size:12px ;font-family: sans-serif ;color: black ;background: #FFFFFF ">Y </td>
<td class="attributes" style="font-size:12px ;font-family: sans-serif ;color: black ;background: #FFFFFF ">2 </td>
<td class="attributes" style="font-size:12px ;font-family: sans-serif ;color: black ;background: #FFFFFF "/>
</tr>
<tr onclick="jsEvent('list item 2)">
<tr onclick="jsEvent('list item 3)">
<tr onclick="jsEvent('list item 4)">
</tbody>
</table>

Obviously this isn't the exact code, but a close enough copy of the HTML, essentially I need to detect if the table is empty, if it isn't empty I need to delete all the entries (however the first entry is headers)

Here is the Selenium I have used so far to try

try{
        Thread.sleep(500);
        //System.out.println("STUFF");
        WebElement deleteName = driver.findElement(By.xpath("html/body/table/tbody/tr[2]/td[2]/img"));
        WebElement rowWithNameEntry = null;
        //System.out.println("STUFF");
        for(int i=2; i>6; i++){
            WebElement nameEntry = driver.findElement(By.xpath(".//*[@id='table']/tbody/tr["+i+"]/td[1]"));
            rowWithNameEntry = nameEntry.findElement(By.xpath("./.."));
            System.out.println("stuff");
            rowWithNameEntry.click();
            deleteName.click();
            Thread.sleep(100);
            new Actions(driver).sendKeys(Keys.RETURN).perform();
            Thread.sleep(100);
        }

When this didn't work I also tried this approach.

WebElement mainTable = driver.findElement(By.id("table"));
        List<WebElement> nameEntries = mainTable.findElements(By.xpath(".//*[@id='table']/tbody/tr[2]"));
        for (WebElement nameEntry : nameEntries) {
            nameEntry.click();
            deleteName.click();
            Thread.sleep(100);
            new Actions(driver).sendKeys(Keys.RETURN).perform();
            Thread.sleep(100);

In both cases (and the fifty others I tried) even with 4 table entries the program just skips over the try / catch and then breaks when it tried to enter a 5th entry.

Any assistance would be fantastic,

Thank you all, Farrell

PS (On a side note, if I wished to select by the "BIGNAME" attribute in the how would that be done?)

No correct solution

OTHER TIPS

Have you tried getting the table data into an ArrayList ? . May be you can use that arraylist for your further usage and also remove the list elements you dont want. I am also very much new to selenium and if i am not wrong then I dont think selenium allows you to manipulate the table or html id on the web browser. You can send keys and as such but deleting rows ? .. not so sure. so unless there is a way provided (something which enables you to delete rows) I dont think you can delete row. Please let me know if this works for you or not.

Firstly you could get all the rows of the table like this:

List<WebElement> rows = we.findElements(By.xpath(".//tbody/tr"));

then you could iterate over all the rows and then get all the columns of a row like this:

List<WebElement> cols = rows.get(rowID).findElements(By.xpath(".//td"));

To check the "BIGNAME", iterate over the cols and do this :

cols.get(index).getText().equals("BIGNAME")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top