循环播放列表以单击每个列表项

时间:2014-07-30 15:07:54

标签: c# selenium selenium-webdriver webdriver

我试图绕过广播列表并依次点击每个列表项。以下是该页面的代码:

<div><input type="radio" name="voiceSpeed" class="rtSlow"><span i18n-content="​&quot;voiceSpeedSlow&quot;">&nbsp;​Slow</span><br><input type="radio" name="voiceSpeed" value="true" checked=""><span i18n-content="​&quot;voiceSpeedMedium&quot;" class="rtMedium">&nbsp;Medium​</span><br><input type="radio" name="voiceSpeed" class="rtFast"><span i18n-content="​&quot;voiceSpeedFast&quot;">&nbsp;Fast​</span><br></div>

这就是我的想法(它运作正常,但这就是问题。它还不错。我只是想要一些关于如何简化它的建议/想法)

我正在使用c#

IwebElement SlowRadionButton = driver.FindElement(By.ClassName("rtSlow"));
IwebElement MediumRadionButton = driver.FindElement(By.ClassName("rtMedium"));
IwebElement FastRadionButton = driver.FindElement(By.ClassName("rtFast"));

Object[] SpeedRadioButtons = new Object[] { SlowRadioButton. MediumRadioButton, FastRadionButton }

for (int 1 = 0; 1 < SpeedRadioButtons.Count(); i++)
{
if (i == 0)
SlowRadioButton.Click();
}
else if (i == 1)
{
MediumRadionButton.Click();
}
else if (i == 2)
{
FastRadionButton.Click();
}

非常感谢您的帮助。非常感谢

1 个答案:

答案 0 :(得分:0)

绝对与Selenium无关。这只是一个纯粹的编程逻辑问题,即“如何遍历列表”。

使用foreach循环:

IWebElement SlowRadionButton = driver.FindElement(By.ClassName("rtSlow"));
IWebElement MediumRadionButton = driver.FindElement(By.ClassName("rtMedium"));
IWebElement FastRadionButton = driver.FindElement(By.ClassName("rtFast"));

var speedRadioButtons = new[] { SlowRadioButton. MediumRadioButton, FastRadionButton }

foreach (IWebElement speedRadioButton in speedRadioButtons)
{
    speedRadioButton.Click();
}

(伪代码,未编译)