简体   繁体   English

如何使用C#在Selenium WebDriver中单击列表中的项目?

[英]How to click an item from a list in Selenium WebDriver using C#?

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 情况1输出:如果项目同时满足两个条件

Classname:
text: $189

// shows classname is empty and it entered the loop. //显示类名为空,并进入循环。

case 2 output: if first item doesnot have $ 情况2输出:如果第一个项目没有$

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. 继续对页面上的30个项目重复相同的操作,而不是转到第二个项目。

The inner FindElement appears to want to get itemWithDollarSign related to the looping item , but it appears to actually be static. 内部的FindElement似乎想要获取与循环item相关的itemWithDollarSign ,但实际上似乎是静态的。

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']"));

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

相关问题 如何使用Selenium Webdriver C#从下拉列表中选择列表项的值 - How to select a list item from a drop down by its value using Selenium Webdriver C# 如何使用Selenium WebDriver和C#单击复选框 - How to click the checkbox using Selenium WebDriver and C# 我如何在C#中使用Selenium Webdriver单击图像? - how can i click on image using selenium webdriver in c#? c#如何使用Selenium Webdriver单击按钮? - c# How to click button using selenium webdriver? 如何使用Selenium Webdriver C#在chrome弹出窗口上单击OK - How to Click OK on chrome popup using Selenium Webdriver C# 使用C#的Selenium Webdriver:如何从网页上的URL数量中找到URL,然后单击它? - Selenium Webdriver using C#: How to find a URL from number of URL on a webpage and click on it? Selenium WebDriver中使用C#的项目列表 - List of items in Selenium WebDriver using C# 如何使用Selenium Webdriver C#从弹出式下拉列表中选择一个选项 - how to select an option from the popup dropdown list using selenium webdriver c# 如何使用 Selenium 和 C# 在项目上使用 hover 避免点击它 - How to hover over an item avoiding a click on it using Selenium and C# 如何通过 Selenium WebDriver 和 C# 在列表 (ul) 中选择项目 (li) - 定位时超时 - How to select an item (li) within a list (ul) through Selenium WebDriver and C# - timeout when locating
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM