简体   繁体   English

Selenium WebDriver,从表中删除

[英]Selenium WebDriver, deleting from a table

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. 以下是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. 第一个TR是报头(需要忽略),之后每个TR等于一个条目,并且限制为4个条目。

<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) 显然,这不是确切的代码,而是HTML的足够近的副本,本质上,我需要检测表是否为空,如果不为空,则需要删除所有条目(但是第一个条目是标头)

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. 在这两种情况下(以及我尝试过的其他五十种情况),即使有4个表条目,该程序也只跳过try / catch,然后在尝试输入第5个条目时中断。

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?) PS(顺便说一句,如果我希望通过“ BIGNAME”属性进行选择,该怎么做?)

Have you tried getting the table data into an ArrayList ? 您是否尝试过将表数据放入ArrayList中? . May be you can use that arraylist for your further usage and also remove the list elements you dont want. 可能是您可以使用该arraylist进行进一步使用,还可以删除不需要的list元素。 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. 我对硒也很陌生,如果我没记错的话,我认为硒不会允许您在网络浏览器上操作表格或html id。 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 : 要检查“ BIGNAME”,请遍历cols并执行以下操作:

cols.get(index).getText().equals("BIGNAME")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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