2016-06-28 70 views
2

我是一名初学者,在测试中遇到问题。如果我使用属性FindsBy,如何正确使用ReadOnlyCollection<IWebElement>。开始测试后,我的收藏始终为空。继承人是我在C#代码:Selenium:FindsBy with collection

 [FindsBy(How = How.Name, Using = "role")] 
     public ReadOnlyCollection<IWebElement> radPercentage { get; } 

这里是测试网络:http://testwisely.com/demo/survey

我想要做这样的事情:radPercentage[2].Click();

回答

2

你需要调用InitElements使用前集合。通过驱动程序和包含FindsBy属性的类的实例(在我的代码“this”中)。

IWebDriver driver = new FirefoxDriver(); 
driver.Navigate().GoToUrl("http://testwisely.com/demo/survey"); 
PageFactory.InitElements(driver, this); 
IWebElement radio = this.radPercentage[2]; 

方法InitElements期待的属性为类型的IWebElement的IList IWebElement

[FindsBy(How = How.Name, Using = "role")] 
public IList<IWebElement> radPercentage; 
+0

你们说的IList 作品完美...谢谢 – Deyeth

+0

一旦页面元素初始化,你可以通过使用'PageFactory.InitElements(driver,radPercentage)''刷新一个特定的元素? –

+0

刷新元素不仅需要再次调用该方法,还需要访问该页面。不知道是否有快捷方式。 – derloopkat

0

试试这个。

public void FindStuff() 
{ 
    var stuff = driver.FindElements(By.Name("role")); 
    stuff[2].Click(); 
}