Home > database >  How to collect a Value out of multiple div elements in a list with Selenium in C#?
How to collect a Value out of multiple div elements in a list with Selenium in C#?

Time:11-07

i have a list on a website that stores the part number and the order number. in this list are different div elements and i would like to export every part number in this list.

The list looks like this:

<div class="spareValue">
    <span class="label">OrderNumber:</span>
    <span class="PartNumber">180011</span>
</div>
<div class="spareValue">
    <span class="label">SparePartNumber:</span>
    <span class="PartNumber">01002523</span>
</div>

How can i export every OrderNumber and put them into a list in c# that i can work with the values??

CodePudding user response:

Oke, you want every partnummer, that belongs to an ordernummer. That leads me to this xPath, find the div that has a ordernumber in it, than find the partnumber element inside.
Then to find them all (or none).
Last put them all in a neat list, selecting the partnumber text:

string path = "//div[@class='spareValue' and ./span[text()='OrderNumber:']]/div[@class='PartNumber']"
var elements = driver.findElements(By.XPath(path));
var listOrderNumber = elements.Select(e=>e.Text);

CodePudding user response:

lot of ways to do that:

        var spans = driver.FindElements(By.CssSelector("div.spareValue span"));
        var nbrspans = spans.Count;
        var Listordernumber = new List<string>();
        for(int i = 0; i < nbrspans; i  =2)
        {
            if (spans[i].GetAttribute("textContent") != "OrderNumber:") continue;
            Listordernumber.Add(spans[i   1].GetAttribute("textContent"));
        }

so Listordernumber contains the result

if you prefer linq, you could use that:

string path = "//div[@class='spareValue' and ./span[text()='OrderNumber:']]/span[@class = 'PartNumber']";
var Listordernumber = driver.FindElements(By.XPath(path)).Select(s => s.GetAttribute("textContent")).ToList();
  • Related