简体   繁体   English

从清单项目中放入项目

[英]Shelling an item from a list item

I am automating some website and I got stuck on such a case. 我正在自动化一些网站,却陷入这种情况。 I have a list created, everything is great for me, it has 24 elements. 我创建了一个列表,对我来说一切都很好,它包含24个元素。 These are products from the store, containing their picture, name, price, etc. But now I need to take two things from the first element and display in the console, namely name and price. 这些是商店的产品,包含它们的图片,名称,价格等。但是现在我需要从第一个元素中提取两件事并在控制台中显示,即名称和价格。 Is anyone able to suggest something? 有人可以提出建议吗? I sit and thinks but nothing comes out. 我坐着想,但什么都没出来。 All I managed to do was send everything for 1 item. 我要做的就是将所有物品都寄给1件。

I tried resolve that with some Linq but without success. 我尝试通过一些Linq解决此问题,但没有成功。

public List<string> ListOfElements()
{
    var elements = new List<string>();

    IReadOnlyCollection<IWebElement> listElements = _driver.FindElements(By.CssSelector(".category-list div[class='cat-prod-row js_category-list-item js_clickHashData js_man-track-event ']");

    foreach (IWebElement element in listElements)
    {
        elements.Add(element.Text);
    }

    return elements;
}

public void DisplayFirstElement()
{
    var firstElement = ListOfElements();
    Console.WriteLine(firstElement[0]);
}

I want get name and price of first element and then assert that price for that is greater than 10. 我想获取第一个元素的名称和价格,然后断言该价格大于10。

You´re flattening all the elements properties into a single collection of strings. 您将所有元素属性展平为单个字符串集合。 Thus you´re losing any relationship between those strings. 因此,您将失去这些字符串之间的任何关系。 It´s hard to say what the 22rd element within thhat list actually is: is it a price? 很难说出thhat列表中的第22个元素是什么:价格吗? A name? 一个名字? Something completey different? 完全不同吗? To which item does it actually belong? 它实际上属于哪个项目?

Instead you should just return a list of entities and then print the properties of the very first entitity : 相反,你应该只返回实体的名单,然后再打印第一个entitity的属性:

public List<IWebElement> ListOfElements()
{
    var elements = new List<string>();
    return listElements = _driver.FindElements(By.CssSelector(".category-list div[class='cat-prod-row js_category-list-item js_clickHashData js_man-track-event ']");
}

public void DisplayFirstElement()
{
    var allElements = ListOfElements();
    var firstElement = allElements.First();
    Console.WriteLine("FirstName: " + firstElement.Name + " Price: " + firstElement.Price);
}

Of course this assumes your IWebElement has a Name - and a Price -property. 当然,这假设您的IWebElement具有“ Name和“ Price属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM