Big Hendo
Big Hendo

Reputation: 185

How to find an element and then find another element in selenium?

I am unable to implement a double find element in selenium c sharp. Simply I have an if statement where based on the condition, I select the first base value from the first basket summary, else I select the first base value from the second basket summary.

The code below does not recognise basevalue[0] because of an error stating: iweb element does not contain a definition for'basePrice' and no extension method 'basePrice' accedpt a first argument of type 'IWebElement' could be found. Am I missinga reference.

How am I suppose to implement the code below to meet with what I am trying to cover?

var basketSummary = _driver.FindElements(CommonPageElements.BasketSummaryContent);
var basePrice = _driver.FindElements(CommonPageElements.BasePriceValue);

    if (basketLocation.ToLower() == "top")
    {

        decimal basketSummaryPrice = decimal.Parse(basketSummary[0].basePrice[0].Text, NumberStyles.Currency, _ci);


        return basketSummaryPrice;
    }
    else
    {

        decimal basketSummaryPrice = decimal.Parse(basketSummary[1].basePrice[0].Text, NumberStyles.Currency, _ci);


        return basketSummaryPrice;
    }

Upvotes: 0

Views: 58

Answers (1)

Dale
Dale

Reputation: 1941

I think you might have a slight misunderstanding of your error, its not an issue with references. The issue is that when you do basketSummary[0] you have an object of type IWebElement this type does not have a property or function called basePrice. In this case basePrice is separate collection of 'IWebElemet'

I think what you are trying to do is call FindElements(CommonPageElements.BasePriceValue) on the basketSummary[0].

I don't have an IDE handy, so I can't promise 100% accurate code but try something like this:

var basketSummary = _driver.FindElements(CommonPageElements.BasketSummaryContent);

if (basketLocation.ToLower() == "top")
{

    decimal basketSummaryPrice = decimal.Parse(basketSummary[0].FindElements(CommonPageElements.BasePriceValue)[0].Text, NumberStyles.Currency, _ci);


    return basketSummaryPrice;
}
else
{

    decimal basketSummaryPrice = decimal.Parse(basketSummary[1].FindElements(CommonPageElements.BasePriceValue)[0].Text, NumberStyles.Currency, _ci);


    return basketSummaryPrice;
}

You will notice the slight differences in the lines:

decimal basketSummaryPrice = decimal.Parse(basketSummary[0].FindElements(CommonPageElements.BasePriceValue)[0].Text, NumberStyles.Currency, _ci);

and

decimal basketSummaryPrice = decimal.Parse(basketSummary[1].FindElements(CommonPageElements.BasePriceValue)[0].Text, NumberStyles.Currency, _ci);

I have also removed the line that declares var basePrice as it's not needed.

Upvotes: 1

Related Questions