简体   繁体   中英

Selenium C# unable to get the list using FindElements

0 ,C# and NUnit.

The problem I am facing is that I need to fetch out the value from a span and store into an arraylist.

I am Using the below code to fetch the price list

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

from the HTML given below

  <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> 

I do get the list but am unable to fetch the text from the elements I got using the css selector.

Below is the watch from visual studio.

    -       [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

I need to fetch the 'Text' with price given in the above watch.

Sorry if its unclear .I am a bit new to this.

Finally Fixed it.

The issue was that the var used to store elements stored elements as an arraylist and had to find the position and then dig into the Text property. Below is the corrected code-

    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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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