如何在webdriver脚本中增加Xpath?

时间:2015-06-24 06:59:27

标签: selenium

driver.findElement(By.xpath(".//*@id='MyGrid']/tbody/tr[1]/td/table/tbody/tr/td[2]/table/tbody/tr/td[2]/table/tbody/tr[1]/td[3]/b")).getText();

.//*@id='MyGrid']/tbody/tr[2]/td/table/tbody/tr/td[2]/table/tbody/tr/td[2]/table/tbody/tr[1]/td[3]/b
.//*[@id='MyGrid']/tbody/tr[3]/td/table/tbody/tr/td[2]/table/tbody/tr/td[2]/table/tbody/tr[1]/td[3]/b

需要增加xpath以上并执行操作。

1 个答案:

答案 0 :(得分:1)

Use a for loop

get total xpaths list size

    List<WebElement> xpaths = driver.findElements(By.xpath("//*[@id='MyGrid']/tbody/tr"));

    //iterate through the list

        for(int i=0;i<xpaths.size();i++){

            driver.findElement(By.xpath("//*[@id='MyGrid']/tbody/tr["+i+"]/td/table/tbody/tr/td[2]/table/tbody/tr/td[2]/table/tbody/tr[1]/td[3]/b"));//perform actions
        }

    }