XPath-从具有动态ID的表中获取元素

时间:2015-11-05 07:56:57

标签: java selenium xpath selenium-webdriver

我正在尝试使用Selenium为OBIEE应用程序自动化我的测试用例。现在,我需要从生成的表格报告中读取一个值。问题是,总数最后一个单元格的ID不断变化。 例如 - 目前id为:db_saw_9270_6_1610_0。 刷新后,ID将成为其他内容。中间的4个数字(9270)发生变化。剩下的一点是一样的。我正在使用以下逻辑来捕获此元素:

driver.findElement(By.xpath(".//*[contains(@id, '_6_1610_0')]")).getText();

但是,它返回org.openqa.selenium.NoSuchElementException:无法找到元素:

请告诉我哪里出错了,我该怎么办?

3 个答案:

答案 0 :(得分:0)

您可以尝试starts-withsubstring(代替xpath 2.0 methdod ends-with):

string xpath = "//*[starts-with(@id, 'db_saw_') and substring(@id, string-length(@id) - 8) = '_6_1610_0']"

driver.findElement(By.xpath(xpath)).getText();

答案 1 :(得分:0)

您可以在xpath下面尝试: -

driver.findElement(By.xpath("//*[starts-with(@id, 'db_saw')]")).getText();

答案 2 :(得分:0)

driver.findElement(By.CSSselector("a[id*='_6_1610_0']")).getText();

注意:a代表一个html元素。如果您的ID位于元素中,则必须按表替换a

Check this out for more examples with css selector

相关问题