【发布时间】: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