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

时间:2015-01-28 12:50:13

标签: java selenium

我正在尝试为我的应用程序自动化我的测试用例。我已经添加了代码和屏幕截图供您参考。 应用程序中只有一个添加按钮,因此我很容易为add编写一个事件。但是如屏幕所示,存在大量删除按钮。因此,我很难自动化动态删除按钮。现在我正在尝试编写删除所有记录的代码。但是Id和类都没有在html代码中工作。请写。请帮助。

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

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>

1 个答案:

答案 0 :(得分:0)

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

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

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

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

如果这不起作用,您很可能想要考虑<i class="icon-trash">孩子。此xPath定位器将搜索具有子属<a>元素的<i>元素,其中类属性值为icon-trash

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

另请注意,这些元素都不具有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);
 }