在单选列表周围循环以单击每个列表项

本文关键字:列表 单击 循环 单选 周围 | 更新日期: 2023-09-27 18:15:44

我正在尝试循环一个单选列表,并依次单击每个列表项。下面是该页面的代码:

<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();
}

非常感谢你的帮助。感谢

在单选列表周围循环以单击每个列表项

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

使用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();
}

(伪代码,未编译)