C# 可以在列表中放置多个SelectElements
我想知道是否可以将多个C# 可以在列表中放置多个SelectElements,c#,selenium,C#,Selenium,我想知道是否可以将多个selectItems放在一个列表中,这样我就可以浏览它们并随机选择一些内容 这就是我到目前为止所做的: IList<SelectElement> allLifeStyleDD = (IList<SelectElement>)driver.FindElements(By.XPath(lifeStyleDD)); Console.WriteLine("Size: " + allLifeStyleDD.Count); var random = new
selectItems
放在一个列表中,这样我就可以浏览它们并随机选择一些内容
这就是我到目前为止所做的:
IList<SelectElement> allLifeStyleDD = (IList<SelectElement>)driver.FindElements(By.XPath(lifeStyleDD));
Console.WriteLine("Size: " + allLifeStyleDD.Count);
var random = new Random();
var options = new List<string>{"Never", "Daily", "Weekly",
"Monthly", "Quarterly", "Annually"};
foreach (SelectElement element in allLifeStyleDD)
{
int index = random.Next(options.Count);
Console.WriteLine("Size: " + allLifeStyleDD.Count);
Console.WriteLine("About to set: " + options[index]);
element.SelectByText(options[index]);
}
IList allLifeStyleDD=(IList)driver.FindElements(By.XPath(lifeStyleDD));
Console.WriteLine(“大小:+allLifeStyleDD.Count”);
var random=新的random();
var options=新列表{“从不”、“每日”、“每周”,
“月度”、“季度”、“年度”};
foreach(allLifeStyleDD中的SelectElement)
{
int index=random.Next(options.Count);
Console.WriteLine(“大小:+allLifeStyleDD.Count”);
Console.WriteLine(“即将设置:“+options[index]);
元素。按文本选择(选项[索引]);
}
但是我得到一个错误,说明指定的强制转换无效
其思想是每次执行时随机分配所有找到的下拉列表。这是引发异常的行:
IList<SelectElement> allLifeStyleDD =
(IList<SelectElement>)driver.FindElements(By.XPath(lifeStyleDD));
我发现了一种不同的方法,我发现在一个列表中放置多个下拉对象很困难,但我相信这是可能的,所以我可以稍后再试 然而,我发现我想与之交互的每个dropbox都有相同的id,但末尾的数字是递增的 所以,最后,我做到了
var random = new Random();
var options = new List<string> { "Never", "Daily", "Weekly", "Monthly", "Quarterly", "Annually" };
for (int i = 1; i < 23; i++)
{
int index = random.Next(options.Count);
general.waitForElementAndSelectDD("ctl00_ContentPlaceHolder1_ddl" + i.ToString(), options[index]);
}
我希望这对某人有所帮助你是说你有多个下拉列表,你想从其中任何一个中随机选择?欢迎来到stack@BrianMitchell:)哪一行抛出了错误?我有多个下拉列表,我想随机设置所有下拉列表Brian,在budi回答中的评论中,你说你发现了一些有效的东西。为什么不把它贴出来作为其他有同样需求的人的答案呢?Will do Mark,所有这些都完成了。这给了我同样的错误,我最终采取了不同的方法,很抱歉,你是对的。我已经相应地更新了我的LINQ解决方案。
var random = new Random();
var options = new List<string> { "Never", "Daily", "Weekly", "Monthly", "Quarterly", "Annually" };
for (int i = 1; i < 23; i++)
{
int index = random.Next(options.Count);
general.waitForElementAndSelectDD("ctl00_ContentPlaceHolder1_ddl" + i.ToString(), options[index]);
}
public void waitForElementAndSelectDD(String dropdown, String option){
new WebDriverWait(driver,
TimeSpan.FromSeconds(longTimeout))
.Until(ExpectedConditions.ElementExists((By.Id(dropdown))));
SelectElement dropDown = new SelectElement(driver.FindElement(By.Id(dropdown)));
dropDown.SelectByText(option);
}