如何在selenium中处理动态Id?

时间:2016-08-23 11:54:26

标签: xpath selenium-webdriver

我正在研究自动化并且每次都获得动态ID。请找到示例代码..

<li class="list-group-items pingMessage clearfix" id="59580" data-reactid=".c.$0.0.$59580"></li>
<li class="list-group-items pingMessage clearfix" id="59581" data-reactid=".c.$0.0.$59580"><li>
<li class="list-group-items myMessage pingMessage clearfix bunch" id="59588" data-reactid=".e.$0.0.$59578"></li>

每个<li>项都有不同的ID。我希望使用gettext获取商品的ID值,然后将其存储在变量中并调用xpath

我尝试了以下代码:

//Getting id of particular text 
WebElement Id=driver.findElement("By.xpath(//*[@class='list-group-items myMessage pingMessage clearfix bunch']").getText();
//My problem starts here i want to pass the stored id as id value how can i do it..

driver.findElement("By.id("+Id+")).click;

2 个答案:

答案 0 :(得分:0)

getText()用于获取此元素的可见(即CSSinnerText,包括子元素,没有任何前导或尾随空格,而您需要此处获取元素id属性,因此您应该尝试使用getAttribute(),如下所示: -

//Storing the value of ID
WebElement Id = driver.findElement(By.cssSelector(".list-group-items.myMessage.pingMessage.clearfix.bunch")).getAttribute("id");

答案 1 :(得分:0)

请尝试使用以下xpath

//tagName[contains(@id='595')]
相关问题