【问题标题】:How to locate and click on a record from a dynamic table in selenium webdriver如何在 selenium webdriver 的动态表中定位并单击记录
【发布时间】:2017-05-15 00:22:49
【问题描述】:

我的代码在下面哪个有效。有人可以提出更好的方法吗?

WebElement tasktable=driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[3]/div/div[3]/div/div[2] /div/div[3]/div/div[2]/div/div[3]/div/div[3]/div/div[2]/div/div/table/tbody"));

List<WebElement> rows=tasktable.findElements(By.tagName("tr"));

System.out.println("Number of rows:"+rows.size());
String sColValue="Test1";
//String sRowValue="Status";
System.out.println("Searching for : "+ sColValue);  
 for(int rnum=1;rnum<=rows.size();rnum++)

    {

String sValue = null;
String rValue=null;
sValue = driver.findElement(By.xpath("html/body/div[2]/div[2]/div/div[3]/div/div[3]/div/div[2]/div/div[3]/div/div[2]/div/div[3]/div/div[3]/div/div[2]/div/div/table/tbody/tr["+rnum+"]/td[2]")).getText();
Thread.sleep(3000);
System.out.println("Name:"+ sValue);    
if(sValue.equalsIgnoreCase(sColValue))

    {
    System.out.println("Found : "+ sValue); 
    rValue = driver.findElement(By.xpath("html/body/div[2]/div[2]/div/div[3]/div/div[3]/div/div[2]/div/div[3]/div/div[2]/div/div[3]/div/div[3]/div/div[2]/div/div/table/tbody/tr["+rnum+"]/td[2]")).getText();
    System.out.println("Status:"+ rValue);
    Actions action= new Actions(driver);
    action.moveToElement(driver.findElement(By.xpath("html/body/div[2]/div[2]/div/div[3]/div/div[3]/div/div[2]/div/div[3]/div/div[2]/div/div[3]/div/div[3]/div/div[2]/div/div/table/tbody/tr["+rnum+"]/td[2]"))).doubleClick().build().perform();
    new Actions(driver).doubleClick(driver.findElement(By.xpath("html/body/div[2]/div[2]/div/div[3]/div/div[3]/div/div[2]/div/div[3]/div/div[2]/div/div[3]/div/div[3]/div/div[2]/div/div/table/tbody/tr["+rnum+"]/td[2]"))).perform();
    Thread.sleep(3000);
    break;
    }
else {
    System.out.println("Name:"+ sColValue+ "is not found"); 
}

【问题讨论】:

  • 你使用绝对的XPath。这不是一个好主意。显示目标元素的HTML 以获得不错的相对XPath :) ..或其他更灵活的选择器。你也可能需要检查这个w3schools.com/xml/xpath_intro.asp
  • 下面是 HTML
  • Referral Approved
    是表中的行之一

标签: javascript selenium automated-tests


【解决方案1】:

要查找 cmets 元素中提供的内容,请使用 XPath:

//div[text()="Referral Approved"]

【讨论】:

  • 代码必须检查表中每一行的值,并在值匹配时选择记录
猜你喜欢
  • 1970-01-01
  • 2012-06-02
  • 1970-01-01
  • 1970-01-01
  • 2023-01-18
  • 1970-01-01
  • 2016-08-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多