1

I have a problem of filling in this dictionary with a result from HTML table. HTML looks something like this:

  <td class="name">
     <span data-bind="text:'hierarchyId'"></span>
     <span data-bind="text:'name'"></span> 
   </td>

I want to fill in dictionary with HiearchyId and name for every td, but it ends up filling only first column and in second iteration return error that same ID already exist in dictionary. Please advice.

  public Dictionary<string,string> ListOfSections()
    {
        Dictionary<string,string> newDictionary = new Dictionary<string,string>();
        IList<IWebElement> tdSectionName = sectionTable.FindElements(By.XPath("//td[@class='name']"));
        foreach (IWebElement element in tdSectionName)
        {
            IWebElement hierarchy = element.FindElement(By.XPath("//span[@data-bind='text: hierarchyId']"));
            IWebElement name = element.FindElement(By.XPath("//span[@data-bind='text: Name']"));
            newDictionary.Add(hierarchy.Text, name.Text);
        }
        return newDictionary;
    }
3
  • foreach (IWebElement element in tdName) should you use tdSectionName list? I don't know from where you are getting the tdName. Commented Mar 16, 2019 at 22:21
  • can you also make sure the html is correct, we don't see the data-bind attribute for the span. Commented Mar 16, 2019 at 22:23
  • you are right. I am using tdSectionName acutally but edited the code before upload. Html is not complete. I just wanted to point that there are two span inside one td element so I sketched the html. But yes there is data-bind attribute in the html.Ty for you comment Commented Mar 16, 2019 at 23:29

1 Answer 1

2

Add .// to xpath to get child element like below, or use css selector:

public Dictionary<string,string> ListOfSections()
{
    Dictionary<string,string> newDictionary = new Dictionary<string,string>();
    IList<IWebElement> tdSectionName = sectionTable.FindElements(By.XPath("//td[@class='name']"));
    foreach (IWebElement element in tdName)
    {
        IWebElement hierarchy = element.FindElement(By.XPath(".//span[@data-bind='text: hierarchyId']"));
        IWebElement name = element.FindElement(By.XPath(".//span[@data-bind='text: Name']"));
        newDictionary.Add(hierarchy.Text, name.Text);
    }
    return newDictionary;
}

Css selector:

public Dictionary<string,string> ListOfSections()
{
    Dictionary<string,string> newDictionary = new Dictionary<string,string>();
    IList<IWebElement> tdSectionName = sectionTable.FindElements(By.CssSelector("td.name"));
    foreach (IWebElement element in tdName)
    {
        IWebElement hierarchy = element.FindElement(By.CssSelector("span[data-bind='text: hierarchyId']"));
        IWebElement name = element.FindElement(By.CssSelector("span[data-bind='text: Name']"));
        newDictionary.Add(hierarchy.Text, name.Text);
    }
    return newDictionary;
}
Sign up to request clarification or add additional context in comments.

5 Comments

An needs to check if hierarchy and name is not null!. is it true?
Explain, what do you mean null?
It is possible that parent element has no span[data-bind='text: hierarchyId'] or span[data-bind='text: Name'] child?
@isaeid you should know it. If not found you'll get an error
@runningman you can use element.FindElements(By.CssSelector("span[data-bind='text: hierarchyId']")) and check size. If >0 than element exit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.