如何从元素中选择elementS。例如,我有下一个元素div块,并希望从这个div块中选择所有类型等于radio的元素:
private static readonly By divBlock =By.XPath("//*[@contains(@id, 'qst_8220_qst_8235')]");
private static readonly By elementType = By.XPath(".//*[@type='radio']");
此代码对我不起作用:
foreach (var elem in WebDriver.FindElement(divBlock).FindElements(elementType))
{
elem.Click();
}
错误:对象引用未设置为对象的实例。
答案 0 :(得分:0)
尝试这种方式:
IWebDriver Driver = new (select some driver);
var divBlock = Driver.FindElement(By.Xpath("//*[@contains(@id, 'qst_8220_qst_8235')]"));
var elementType = Driver.FindElements(By.XPath(".//*[@type='radio']"));
foreach (var elem in elementType.Count)
{
elem.Click();
}
答案 1 :(得分:0)
IWebElement有方法findelements。所以你可以尝试这种方式
private static readonly By divBlock =By.XPath("//*[@contains(@id, 'qst_8220_qst_8235')]");
private static readonly By elementType = By.XPath(".//*[@type='radio']");
IWebElement Block = Driver.FindElement(divBlock);
List<IWebElement> elementTypes = Block.FindElements(elementType);
Console.WriteLine(elementTypes.count);
foreach (var elem in elementTypes)
{
elem.Click();
}