简体   繁体   中英

How do I check if element/text is present in listbox using selenium webdriver C#

I am sure this question may have been asked before but nothing on here seem to work for me. I need to create a simple loop that go through a list and searches for a specific text. When the text is found it should be deleted, but if it is not present, it could be added. Below is the code I created. the text I am searching for is present in the list, but it seems to pass through without detecting whether the text exists or not.

IList<IWebElement> boxList = _driver.FindElements(By.Id("ListBox"));
foreach (IWebElement i in boxList)
{
   //Assert.AreEqual(i.Text, boxList.Contains("TEST"));

   if (i.Text.Contains("TEST"))
   {
      // value is present in box list
   }
   else
   {
      _driver.FindElement(By.Id("AddButton")).Click();
      var newRecordInfo = table.CreateSet<FeatureInfo>();
      foreach (var recordData in newRecordInfo)
      {
            _driver.FindElement(By.Id("DesTextBox")).SendKeys(recordData.Test_discription);
            _driver.FindElement(By.Id("ScoreTextBox")).SendKeys(recordData.Test_score);

         new SelectElement(_driver.FindElement(By.Id("TypeDropDown"))).SelectByValue("1");
         _driver.FindElement(By.Id("SaveButton")).Click();
      }
   }
}

Here is the HTML:

<select size="10" name="ListBox" id="ListBox" onclick="ListBox_Click()" style="width:98%;display:block;margin-bottom:10px">
        <option value="10" UseCount="0" Score="170" FirearmType="2">TRAINING</option>
        <option value="9" UseCount="0" Score="0" Type="1">TRAINING ONE</option>
        <option value="12" UseCount="0" Score="0" Type="1">TRAINING TWO</option>
        <option value="5" UseCount="5" Score="0" FirearmType="2">TRAINING THREE</option>
        <option value="1" UseCount="31" Score="225" Type="1">TRAINING FOUR</option>     
        <option value="1" UseCount="0" Score="50" Type="1">TEST</option>

It seems that your initial element check is incorrect. Correct me if I am wrong, but:

IList<IWebElement> boxList = _driver.FindElements(By.Id("ListBox"));

is only returning one element? If you want a list of the option elements, you are going to want to add the option tag into the FindElements check.

IList<IWebElement> boxList = _driver.FindElements(By.CssSelector("select#ListBox option"));

With this element list, it should return a list of every single option element, getting the text of each.

EDIT

As per my comment below, I think the problem is that you are doing the text check and add for each option tag, but the functionality you are looking for is that the add is only done once all the options are checked.

IList<IWebElement> boxList = _driver.FindElements(By.CssSelector("select#ListBox option"));
bool textExists = false;

foreach(var option in boxList)
{
    if(option.Text.Contains("TEST"))
    {
        textExists = true;
        break;
    }
}

if(!textExists)
{
    _driver.FindElement(By.Id("AddButton")).Click();
    var newRecordInfo = table.CreateSet<FeatureInfo>();

    foreach (var recordData in newRecordInfo)
    {
         _driver.FindElement(By.Id("DesTextBox")).SendKeys(recordData.Test_discription);
         _driver.FindElement(By.Id("ScoreTextBox")).SendKeys(recordData.Test_score);

         new SelectElement(_driver.FindElement(By.Id("TypeDropDown"))).SelectByValue("1");
         _driver.FindElement(By.Id("SaveButton")).Click();
    }
}

EDIT V2 - Updated bool check

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM