こんにちは、クリックしようとしているアイテムにドル記号が付いていて、在庫がないかどうかを確認する必要があります。リストの最初のアイテムが両方の条件を満たしている場合、これは正常に機能しますが、最初のアイテムにドル記号がない場合、2番目のアイテムに移動してクリックせず、代わりに失敗します. 私はプログラミングが初めてなので、コードに間違いがあるかどうかわかりません。どうすればこれを修正できますか? ありがとう。
コード:
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;
}
}
}
ケース 1 出力: アイテムが両方の条件を満たす場合
Classname:
text: $189
// classname が空であることを示し、ループに入った。
ケース 2 出力: 最初の項目に $ がない場合
Classname:
text: Prices varies
Classname:
text: Prices varies
Classname:
text: Prices varies...
2 番目のアイテムに移動する代わりに、ページ上の 30 アイテムに対して同じことを繰り返し続けます。