1

Hi I need to verify if the item I'm going to click has a dollar sign and is not out of stock. So I have used this method.This works fine if the first item on the list satisfies both the condition but in case if the first item doesn't have a dollar sign it does not go to the second item and click it, instead it fails. As I'm new to programming I don't know if I have made any mistake in the code. How can I correct this ? Thanks.

Code:

        ReadOnlyCollection<IWebElement> listOfItems = driver.FindElements(By.CssSelector("ul[class='shelf-list tap-list search']>li"));
            foreach (IWebElement item in listOfItems)
            {
                //To check if item is not sold out get the class attribute of item and check if empty
                string className = item.GetAttribute("class");
                Console.WriteLine("Classname:" + " " + className);
                if (className.Equals(""))
                {
                    //Item is available and now get text and check if it has $ sign
                    IWebElement itemWithDollarSign = driver.FindElement(By.CssSelector("div[class='item-preview-text']>div[class='price']"));
                    string ItemToChoose = itemWithDollarSign.Text;
                    Console.WriteLine("Text:" + " " + ItemToChoose);
                    if (ItemToChoose.Contains("$"))
                    {
                        //Choose the item that satifies both conditions
                        itemWithDollarSign.Click();
                        break;
                    }
                }
            }

Case 1 output: if item satisfies both condition

Classname:
text: $189

// shows classname is empty and it entered the loop.

case 2 output: if first item doesnot have $

Classname:
text: Prices varies
Classname:
text: Prices varies
Classname:
text: Prices varies...

keeps repeating the same for 30 items on page instead of going to second one.

6
  • I would put a break point on the line "string className = item.GetAttribute ..." and make sure it is satisfying the following if statement. Commented Jun 5, 2013 at 16:41
  • Yup I tried using console.writeline at different places to check if it gets the class name as empty and also if it gets the text and verify's for $. I have updated the code on where i have used the print and the text output. Commented Jun 5, 2013 at 16:49
  • I may not be reading your code correctly, but it appears the line, driver.FindElement(By.CssSelector("div[class='item-preview-text']>div[class='price']")); never changes so it will always find the same first item that satisfies the condition. Commented Jun 5, 2013 at 17:33
  • yes it actually looks up the same item again. But acc. to foreach loop is it not like for each item in the list look up this code. because for all 30 items in the list this is the code to look up $ sign. Do u think the foreach is not executed then ? Commented Jun 5, 2013 at 17:39
  • Can you tell me any other way of writing this concept. Commented Jun 5, 2013 at 17:39

1 Answer 1

3

The inner FindElement appears to want to get itemWithDollarSign related to the looping item, but it appears to actually be static.

This library isn't my bread and butter, but could this be it:

IWebElement itemWithDollarSign = item.FindElement(By.CssSelector("div[class='item-preview-text']>div[class='price']"));
Sign up to request clarification or add additional context in comments.

1 Comment

Wow that's Awesome. My code works now. It chooses the second item. Thank you very much. I knew it shld be related to item but didnt know this was how to do it. thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.