Selenium getattribute list c #

-1

I have a problem creating a list with attributes follows HTML code:

 <div class="post  clearfix" data-post-id="92842173">...</div>
 <div class="post  clearfix" data-post-id="92841636">...</div>
 <div class="post  clearfix" data-post-id="92618462">...</div>
 <div class="post  clearfix" data-post-id="90658834">...</div>

Current C # code:

var valor = chromeDriver.FindElement(By.XPath("//div[contains(@class, 'post')]")).GetAttribute("data-post-id"); 

So I only get the first data-post-id="92842173" .

I would like to create a list with all data-post-id . I have tried to do it as follows, but it gives an error.

List <IWebElement> list1  = chromeDriver.FindElement(By.XPath("//div[contains(@class, 'post')]")).GetAttribute("data-post-id"); 
    
asked by anonymous 08.11.2015 / 04:49

1 answer

0

Come on, there is a method in Selenium to be able to pick up more than one element using the same reference, in this case the path you used to find the first one:

By.XPath("//div[contains(@class, 'post')]");

Instead of using

List <IWebElement> list1  = chromeDriver.FindElement(By.XPath("//div[contains(@class, 'post')]"))

you will use

IList<IWebElement> list1 = chromeDriver.FindElements(By.XPath("//div[contains(@class, 'post')]"));

Now, since you have a list with all these IWebElement , it's now necessary to extract the data-post-id from each, let's suppose you want to store them in a List , so it will look like this:

List<string> dataPost = new List<string>();

for(int i = 0; i < list1.Count(); i++){
    dataPost.Add(list1[i].GetAttribute("data-post-id"));
}
    
04.01.2016 / 13:57