Paski7
Paski7

Reputation: 41

Selenium C# List

Hello Stackoverflow Users,

I have a internet site with 99 list elements. The diffrence between the elements are only the names.

<li class="_6e4x5">
  <div class="_npuc5">
    <div class="_f5wpw">
      <div class="_eryrc">
        <div class="_2nunc">
          <a class="_2g7d5 notranslate _o5iw8" title="Name1" href="/"Name1/">"Name1</a>
        </div>
      </div>
    </div>
  </div>
</li>

[...]

<li class="_6e4x5">
  <div class="_npuc5">
    <div class="_f5wpw">
      <div class="_eryrc">
        <div class="_2nunc">
          <a class="_2g7d5 notranslate _o5iw8" title="Name99" href="/"Name99/">"Name99</a>
        </div>
      </div>
    </div>
  </div>
</li>

What I want:

I want to take the "title" of each list element and put it in a new list.

What I tried:

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

By name = By.XPath("//div[@class='_2nunc']");

IJavaScriptExecutor js = driver as IJavaScriptExecutor;

IList<IWebElement> displayedOptions = driver.FindElements(name);

foreach (IWebElement option in displayedOptions)
{
  string temp = displayedOptions[i].ToString();
  following.Add(temp);
  i++;
}

If I run the code, I only get the element ID, and not the "title" (name34 for example). I hope you have enough information to help me with my problem. Thanks in advance for every help!

Upvotes: 2

Views: 426

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193178

To take the title of each list element and put it in a new list you can use the following code block :

List<string> following = new List<string>();
IList<IWebElement> displayedOptions = driver.FindElements(By.XPath("//li[@class='_6e4x5']//a[@class='_2g7d5 notranslate _o5iw8']"));
foreach (IWebElement option in displayedOptions)
{
    string temp = option.GetAttribute("title");
    following.Add(temp);
}

Upvotes: 1

Neil
Neil

Reputation: 1633

You're looking to get the a element's title attribute. The selenium IWebElement interface has a GetAttribute method you can use to get the title of your elements.

foreach (IWebElement option in displayedOptions)
{
  following.Add(option.GetAttribute("title"));
}

Upvotes: 0

Related Questions