0

I've tried using webelement(and IWebElement) and string lists, but i keep getting errors. How I do get a list or strings of all the elements text by XPath? I have all Selenium references. Do i need some java.util dll? Should I implement a foreach loop?

Pramod
  • 1
  • 1
  • 2
  • what errors? `"i've tried"`.. could you share the code that you have tried? – ddavison Feb 17 '16 at 03:43
  • public List policy1Details = new List();public void PolicySummary1(int i) { policy1Details.Clear(); //var psummary = driver.FindElements(By.XPath("//ul[contains(@class,'discount-list')]/li")); var psummary = driver.FindElement(By.XPath("//div[@id='PolicyDetails_" + i + "']/div/table")); foreach (iwebelement d in psummary) { policy1details.add(d.text); } } – Pramod Feb 17 '16 at 03:58
  • Based on your provide code snipet, you used wrong code to return a list of objects. That's the reason why you have exception/error. – Nguyen Vu Hoang Feb 17 '16 at 04:23
  • Why would you need java.util dll in C# project? – Guy Feb 17 '16 at 05:57

2 Answers2

0

I don't know what exactly you want to do but you can get element text using following code.

  public List<String> policy1Details = new List<String>();
    public void PolicySummary1(int i)
    {
        //var driver = new FirefoxDriver();

        policy1Details.Clear();
        var psummary = driver.FindElements(By.XPath("//text()"));//give your xPath.

        //var psummary = driver.FindElement(By.XPath("//div[@id='PolicyDetails_" + i + "']/div/table"));
        foreach (IWebElement d in psummary)
        {
            //resultText.Add(d.Text);
            policy1Details.Add(d.Text);
        }
    }

if any issue the let me know.

Muhammad USman
  • 208
  • 1
  • 6
0

Here is the topic with a similar question! - XPath to get all child nodes (elements, comments, and text) without parent

And here is the quote from there!

child::* selects all element children of the context node

So you can do the:

var childs = parent.findElements(By.xpath("./child::*"));

Also here is the documentation of XPath, you can read more here!

STG
  • 93
  • 5