无法单击我的元素所在的 css 路径

unable to click the located css path for my element

我正在尝试为我的测试用例自动化 application.I 已添加代码和屏幕截图供您参考。 应用程序中只有一个添加按钮,因此我很容易为 add.But 编写一个事件,如屏幕所示,存在许多删除按钮。所以我很难自动执行动态删除 button.as 现在我正在尝试编写代码来删除所有 records.But Id 和 class 都没有在 html 中工作作为 written.Please 帮助的代码。

屏幕截图在 http://imgur.com/zgmgKq3 btn inverse 和 icon trash 是 class 我找到的标识符

package framework;
import java.util.List;
import java.util.Properties;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class exceloperation {
    WebDriver driver;
 public Object perform;
    public exceloperation(WebDriver driver){
        this.driver = driver;
    }
    public void perform(Properties p,String operation,String objectName,String objectType,String value) throws Exception{
        System.out.println("");
        switch (operation.toUpperCase()) {
      /*  case"OPENBROWSER":
         driver = new FirefoxDriver();
      driver.manage().window().maximize();*/
        case "CLICK":
            //Perform click
            driver.findElement(this.getObject(objectName,objectType)).click();
            break;
        case "SETTEXT":
            //Set text on control
            driver.findElement(this.getObject(objectName,objectType)).sendKeys(value);
            break;
             
        case "GOTOURL":
            //Get url of application
            driver.get(value);
            break;
        case "GETTEXT":
            //Get text of an element
            driver.findElement(this.getObject(objectName,objectType)).getText();
            break;
        case "DELETEALL":
         
         /*    WebElement table = driver.findElement(By.id("phContent_gvEstimate"));
       List<WebElement> allRows = table.findElements(By.tagName("tr"));
             for(WebElement row : allRows)
        {
          List<WebElement> cells = row.findElements(By.tagName("td"));
          boolean required_row = true;
        for (WebElement cell : cells) 
          {
            if(required_row)
            {
          List<WebElement> mylinks = cell.findElements(By.tagName("a"));
          System.out.println("num of mylinks : "+mylinks.size());
         for(WebElement mylink : mylinks )
            {
               String linkid= mylink.getAttribute("id");
               Thread.sleep(5000);
                if(linkid.contains("Delete"))
                   {
                    System.out.println(" Pressing : " + linkid + " By Clicking " + driver.findElement(By.id(linkid)));
                driver.findElement(By.id(linkid)).click();
                Thread.sleep(5000);
                Alert alert = driver.switchTo().alert();
                   alert.accept(); 
                   Thread.sleep(5000);
                   driver.navigate().refresh();
                   allRows = table.findElements(By.tagName("tr"));
                   
            }
               else {

                System.out.println("Cannot be deleted");

               }
            }
         }
        }
       }*/
        List<WebElement> del_elements=driver.findElements(By.tagName("a"));
        for(WebElement mylink :del_elements)
        {
            String linkid= mylink.getAttribute("id");
            Thread.sleep(5000);
         System.out.println("Deleting element:" + linkid);
        }
         default:
            break;
        }
    }
     
    /**
     * Find element BY using object type and value
     * @param p
     * @param objectName
     * @param objectType
     * @return
     * @throws Exception
     */
    private By getObject(String objectName,String objectType) throws Exception{
        //Find by xpath
        if(objectType.equalsIgnoreCase("XPATH")){
             
            return By.xpath(objectName);
        }
        //find by class
        else if(objectType.equalsIgnoreCase("CLASSNAME")){
             
            return By.className(objectName);
             
        }
        //find by name
        else if(objectType.equalsIgnoreCase("ID")){
             
            return By.id(objectName);
             
        }
        //Find by css
        else if(objectType.equalsIgnoreCase("CSS")){
             
            return By.cssSelector(objectName);
             
        }
        //find by link
        else if(objectType.equalsIgnoreCase("LINK")){
             
            return By.linkText(objectName);
             
        }
        //find by partial link
        else if(objectType.equalsIgnoreCase("PARTIALLINK")){
             
            return By.partialLinkText(objectName);
             
        }else
        {
            throw new Exception("Wrong object type");
        }
    }
}
<div class="col-xs-12">
<div class="widget-box">
<div class="widget-title">
<div class="widget-content nopadding">
<div id="ProgramTableContainer" style="overflow: auto;">
<div class="table-main-container">
<div class="table-busy-panel-background" style="display: none;"></div>
<div class="table-busy-message" style="display: none;"></div>
<table class="table table-bordered table-striped table-hover data-table">
<thead>
<tbody>
<tr class="table-data-row table-row-even" data-record-key="48">
<td class="child-opener-image-column">
<img class="child-opener-image" title="Projects" src="../../Content/images/Misc/Plus.png">
</td>
<td>
<td>
<span>2015</span>
</td>
<td>
<span>01/26/2015</span>
</td>
<td>
<span>02/17/2015</span>
</td>
<td>
<td>
<a class="btn btn-primary">
</td>
<td>
<a class="btn btn-inverse">
<i class="icon-trash"></i>
</a>
</td>
</tr>
<tr class="table-data-row" data-record-key="49">
<tr class="table-data-row table-row-even" data-record-key="44">
<tr class="table-data-row" data-record-key="45">
<tr class="table-data-row table-row-even" data-record-key="42">
<tr class="table-data-row" data-record-key="46">
<tr class="table-data-row table-row-even" data-record-key="47">
<tr class="table-data-row" data-record-key="43">
</tbody>
</table>

您似乎需要单击此 table 单元格中的 <a> 元素:

<td>
    <a class="btn btn-inverse">
        <i class="icon-trash"></i>
    </a>
</td>

这个元素有什么独特之处?如果您知道带有 btn-inverse class 的任何按钮将始终是删除按钮,请查找该按钮。此 xPath 定位器正在查找其 class 属性值包含 btn-inverse.

的所有 <a> 元素
List<WebElement> del_elements=driver.findElements(By.xPath("//a[contains(@class,'btn-inverse')]"));

如果这不起作用,您很可能需要考虑 <i class="icon-trash"> child。此 xPath 定位器将搜索具有 child <i> 元素且 class 属性值为 icon-trash

<a> 元素
List<WebElement> del_elements=driver.findElements(By.xPath("//a[i/@class='icon-trash']"));

另请注意,这些元素中的 none 具有 id 属性,因此下面代码中的 linkid 字符串将始终为空:

for(WebElement mylink :del_elements)
{
    mylink.Click();
    // do anything else here to handle deleting the entry

    String linkid= mylink.getAttribute("id");
    Thread.sleep(5000);
     System.out.println("Deleting element:" + linkid);
 }