【问题标题】:Why is IF/ELSE statement Not working properly?为什么 IF/ELSE 语句不能正常工作?
【发布时间】:2019-02-28 18:32:18
【问题描述】:

我需要一些帮助...我在使用 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>

首页

<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

【问题讨论】:

  • option.Text 在您预期的时间内等于多少?
  • 也许 option.Text.Equals 需要不区分大小写
  • Use your debugger 看看为什么option.Text.Equals("TEST") 总是false
  • 很可能列表总是空的,检查CssSelector("ListBox option")是否正确。我不知道 Selenium,但我希望 CssSelector("#ListBox option")CssSelector("select option")
  • 看看 IE 中的 HTML... 和 Chrome 中的完全一样吗?我认为他们的某些方面有所不同......

标签: c# selenium if-statement case


【解决方案1】:

尝试另一种检查 OPTION 的方法。我稍微移动了一些东西并简化了代码。我将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);
    }
}

【讨论】:

  • .Any() 是一种改进,但只是装饰性的。 FindElement(By.Id()) 是建议的解决方案。
  • 嗨,抱歉,我花了更长的时间才回复这个问题。但是我在上面的解决方案中遇到了同样的错误。我比较了 IE 和 Chrome 的 HTML,我没有发现任何重大差异,我在原始帖子中添加了两者的 HTML。
【解决方案2】:

您正在检查对象 Text 而不是检查选项变量

使用option == "TEXT" 而不是option.Text.Equals("TEST")

【讨论】:

  • 感谢您的回复,它说“运算符 == 不能应用于 'IWebElement' 和 'Sting' 类型的操作数”
  • 我想他是想说option.text == "TEXT"...他忘了.text。这不会有任何区别,但我认为这就是他的意思......
猜你喜欢
  • 2021-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-13
  • 1970-01-01
  • 2011-03-14
相关资源
最近更新 更多