c# - Selenium C# 无法使用 FindElements 获取列表

标签 c# html css visual-studio selenium

0、C# 和 NUnit。

我面临的问题是我需要从跨度中取出值并存储到数组列表中。

我正在使用下面的代码来获取价目表

var productprice = driver.FindElements(By.CssSelector(".price.mb-10.text-right "));

来自下面给出的 HTML

    <div class="pl-sec">
<ul>
<li class="row-fluid">
<li class="row-fluid">
<div class="span2">
<div class="span7 disc-content">
<div class="span3 price-content">
<div class="price mb-10 text-right"> £14.99</div>
<div class="full-width mb-20">
</div>
</li>
<li class="row-fluid">
<div class="span2">
<div class="span7 disc-content">
<div class="span3 price-content">
<div class="price mb-10 text-right"> £16.99</div>
<div class="full-width mb-20">
</div>
</li>

我确实获得了列表,但无法从使用 css 选择器获得的元素中获取文本。

下面是visual studio的 watch 。

    -       [0] {OpenQA.Selenium.Firefox.FirefoxWebElement} OpenQA.Selenium.IWebElement {OpenQA.Selenium.Firefox.FirefoxWebElement}
+       [OpenQA.Selenium.Firefox.FirefoxWebElement] {OpenQA.Selenium.Firefox.FirefoxWebElement} OpenQA.Selenium.Firefox.FirefoxWebElement
        Displayed   true    bool
        Enabled true    bool
+       Location    {X = 802 Y = 793}   System.Drawing.Point
        Selected    false   bool
+       Size    {Width = 164 Height = 14}   System.Drawing.Size
        TagName "div"   string
        Text    "£16.99"    string

我需要获取上面 watch 中给定价格的“文本”。

抱歉,如果不清楚。我对此有点陌生。

最佳答案

终于修好了。

问题在于,用于存储元素的 var 将元素存储为数组列表,并且必须找到位置,然后挖掘 Text 属性。 以下是更正后的代码-

    driver.FindElement(By.XPath("//div[4]/div[2]/ul/li[2]/label")).Click();
        //This below codes gets the count of product displayed by checking the prince.
       var productprice = driver.FindElements(By.CssSelector(".price.mb-10.text-right "));
        //var productprice = driver.FindElement(By.XPath("//div[@class='price.mb-10.text-right']")).Text;

       string strval = "";
       strval = productprice[5].Text;

关于c# - Selenium C# 无法使用 FindElements 获取列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32230859/

相关文章:

C# iTextSharp 自动调整列宽

javascript - 如何在javascript中过滤逗号分隔的整数值

javascript - 如何在Xcode项目中包含jquery库?

html - 在表格中选择特定的 tr 元素 (CSS)

c# - 以编程方式获取由 *.asmx/js 生成的 JavaScript

c# - 使用 protobuf-net 反序列化消息列表

javascript - 如何为文本区域中的特定字符着色

html - 需要 CSS 定位帮助

c# - 为什么后台垃圾收集有时会暂停我的应用程序,我该如何防止它发生?

javascript - 使用 JavaScript 从超链接加载时的默认下拉值