[英]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.