为什么IF / ELSE语句不能正常工作?

时间:2019-02-28 18:32:19

标签: c# selenium if-statement case

请给我一些帮助... ELSE语句出现问题,代码执行IF括号中的内容,但从不到达ELSE括号。 Chrome可以正常运行,但每次IE都无法运行。这是我的代码:

IWait<IWebDriver> wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(3.00));
IList<IWebElement> boxList = _driver.FindElements(By.CssSelector("ListBox option"));

bool textExists = false;

foreach (var option in boxList)
{
    if (option.Text.Equals("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("DescTextBox")).SendKeys(recordData._discription);
        _driver.FindElement(By.Id("PaTextBox")).SendKeys(recordData._score);

        new SelectElement(_driver.FindElement(By.Id("DropDown"))).SelectByValue("1");
        _driver.FindElement(By.Id("SaveButton")).Click();
    }
}
else 
{
    SelectElement Select = new SelectElement(_driver.FindElement(By.Id("ListBox")));
    Select.SelectByText("TEST");

    _driver.FindElement(By.Id("DeleteButton")).Click();

    IAlert alert = _driver.SwitchTo().Alert();
    alert.Accept();

    //IJavaScriptExecutor js = (IJavaScriptExecutor)_driver;
    //js.ExecuteScript("window.confirm = function(msg) { return true; }");

    Thread.Sleep(1000);


}

这是HTML:

IE

<div class="section-container">
    <div class="section">
        <table border="0" cellpadding="0" cellspacing="0">
            <tr>
                <td style="width:52%" valign="top">
                    <h5 class="section-label">Active Values</h5>
                    <div class="section">
                        <select size="10" name="DescTextBox" id="DescTextBox" onclick="ListBox_Click()" style="width:98%;display:block;margin-bottom:10px">
    <option value="14" UseCount="0" PassingScore="50" FirearmType="1">123ABC</option>
<option value="10" UseCount="0" PassingScore="170" FirearmType="2">BRTP</option>
<option value="9" UseCount="0" PassingScore="0" FirearmType="1">CORE TRAINING ELEMENTS</option>
<option value="12" UseCount="0" PassingScore="0" FirearmType="1">FAM (T&amp;E PISTOL)</option>
<option value="5" UseCount="559" PassingScore="0" FirearmType="2">FAM ONLY (RIFLE)</option>
<option value="1" UseCount="31" PassingScore="225" FirearmType="1">FLETC PPC (NON-CITP)</option>
<option value="2" UseCount="4001" PassingScore="225" FirearmType="1">HHS-OIG PQC</option>
<option value="3" UseCount="603" PassingScore="240" FirearmType="2">HHS-OIG RQC</option>
<option value="6" UseCount="5" PassingScore="0" FirearmType="1">OTHER FEDERAL AGENCIES-PISTOL</option>
<option value="7" UseCount="2" PassingScore="0" FirearmType="2">OTHER FEDERAL AGENCIES-RIFLE</option>
<option value="11" UseCount="0" PassingScore="0" FirearmType="2">RCTP</option>
<option value="16" UseCount="0" PassingScore="50" FirearmType="1">TEST</option>
<option value="15" UseCount="0" PassingScore="50" FirearmType="1">TEXT_DELETE</option>

</select>

CHOME

<div class="section-container">
		<div class="section">
			<table border="0" cellpadding="0" cellspacing="0">
				<tr>
					<td style="width:52%" valign="top">
						<h5 class="section-label">Active Values</h5>
						<div class="section">
							<select size="10" name="DescTextBox" id="DescTextBox" onclick="ListBox_Click()" style="width:98%;display:block;margin-bottom:10px">
		<option value="14" UseCount="0" PassingScore="50" FirearmType="1">123ABC</option>
		<option value="10" UseCount="0" PassingScore="170" FirearmType="2">TRAINING</option>
		<option value="9" UseCount="0" PassingScore="0" FirearmType="1">TRAINING TWO</option>
		<option value="12" UseCount="0" PassingScore="0" FirearmType="1">TRAINING THREE</option>
		<option value="5" UseCount="559" PassingScore="0" FirearmType="2">TRAINING FOUR</option>
		<option value="1" UseCount="31" PassingScore="225" FirearmType="1">TRAINING FIVE</option>
		<option value="16" UseCount="0" PassingScore="50" FirearmType="1">TEST</option>
		<option value="15" UseCount="0" PassingScore="50" FirearmType="1">TEXT_DELETE</option>

更新了帖子,为IE和Chrome添加了HTML

2 个答案:

答案 0 :(得分:0)

尝试另一种检查OPTIONS的方法。我做了一些改动,简化了代码。我将SelectElement拉到顶部,并用它来遍历选项,并使用LINQ对其进行过滤以查看它们是否等于“ TEST”。我用SELECT构建了一个自定义页面,它可以正确地通过ifelse执行。

public void Test()
{
    IWait<IWebDriver> wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(3.00));
    SelectElement Select = new SelectElement(_driver.FindElement(By.Id("ListBox")));

    if (!Select.Options.Where(e => e.Text == "TEST").Any())
    {
        _driver.FindElement(By.Id("AddButton")).Click();
        var newRecordInfo = table.CreateSet<FeatureInfo>();

        foreach (var recordData in newRecordInfo)
        {
            _driver.FindElement(By.Id("DescTextBox")).SendKeys(recordData._discription);
            _driver.FindElement(By.Id("PaTextBox")).SendKeys(recordData._score);

            new SelectElement(_driver.FindElement(By.Id("DropDown"))).SelectByValue("1");
            _driver.FindElement(By.Id("SaveButton")).Click();
        }
    }
    else
    {
        Select.SelectByText("TEST");
        _driver.FindElement(By.Id("DeleteButton")).Click();
        _driver.SwitchTo().Alert().Accept();
        Thread.Sleep(1000);
    }
}

答案 1 :(得分:-1)

您正在检查对象文本,而不是检查选项变量

使用option == "TEXT"代替option.Text.Equals("TEST")