C# 如何使用页面工厂由另一个WebElement查找WebElement

C# 如何使用页面工厂由另一个WebElement查找WebElement,c#,selenium,webdriver,selenium-webdriver,C#,Selenium,Webdriver,Selenium Webdriver,对源代码进行了简要扫描,不确定这方面的最佳实践是什么 例如,假设我有一个页面对象“DummyPage”,它有两个面板元素TopPanel和BottomPanel。每个面板都有一些元素,这些元素由TopPanel.FindElement()而不是driver.FindElement()找到。如何为此应用页面工厂 我知道PageFactory.InitElements(ISearchContext,Object)接受ISearchContext,但是,我不确定如何将其用于一个类中的页面和面板元素 公

对源代码进行了简要扫描,不确定这方面的最佳实践是什么

例如,假设我有一个页面对象“DummyPage”,它有两个面板元素
TopPanel
BottomPanel
。每个面板都有一些元素,这些元素由
TopPanel.FindElement()
而不是
driver.FindElement()
找到。如何为此应用页面工厂

我知道
PageFactory.InitElements(ISearchContext,Object)
接受
ISearchContext
,但是,我不确定如何将其用于一个类中的页面和面板元素

公共类DummyPage{
私人IWebDriver;
公共DummyPage(IWebDriver驱动程序){
this.driver=driver;
}
公共IList DummyLinks{
获取{return driver.FindElements(By.CssSelector(“.some dummy links”);}
}
公共IWebElement托帕内尔酒店{
获取{return driver.FindElement(By.Id(“top panel”);}
}
公共IWebElement底部面板{
获取{return driver.FindElement(By.Id(“底部面板”);}
}
公共IWebElement FoointAppanel{
获取{return TopPanel.findelelement(By.CssSelector(“.something”);}
}
公共IWebElement FooInBottomPanel{
获取{return BottomPanel.FindElement(By.CssSelector(“.something”);}
}
}
公共类DummyPageWithPageFactory{
公共DummyPageWithPageFactory(IWebDriver驱动程序){
PageFactory.InitElements(驱动程序,this);
}
[FindsBy(How=How.CssSelector,使用=“.some dummy links”)]
公共IList DummyLinks{get;private set;}
[FindsBy(How=How.Id,Using=“top panel”)]
公共IWebElement TopPanel{get;私有集;}
[FindsBy(How=How.Id,Using=“bottom panel”)]
公共IWebElement底部面板{get;private set;}
//公共IWebElement foointAppanel{get;private set;}
//公共IWebElement FooInBottomPanel{get;私有集;}
}
如果我对所有实例使用
driver.FindElement()
,并连接所有定位器,我可能会面临另一种情况,即所有定位器都太长,无法在C#属性中使用变量

[FindsBy(How = How.CssSelector, Using = "#top-panel .blah .blah .super-long-blah .something")]
[FindsBy(How = How.CssSelector, Using = "#top-panel .blah .blah .super-long-blah .something-new")]
[FindsBy(How = How.CssSelector, Using = "#bottom-panel .blah .blah .super-long-blah .something")]
见此:


有一个ByChained类可用于链接Bys。我不确定这是否是您想要的,我也不确定C#语法(尽管我知道它在里面),因为我是Java人。如果没有其他东西的话,希望是一个有用的指针。@Ardesco:是的,我知道这个类,但是,我认为
ByChained
的用法看起来像
driver.FindElement(新的ByChained(By.Id(“顶部面板”)、By.ClassName(“某物”),我不知道这对“页面工厂”有什么帮助。但实际上,这个类位于
PageObjects
名称空间中,这个问题是特定于语言的。我假设.NET绑定有类似的语法,因为ByChained是作为Java页面工厂代码的一部分引入的。我承认我在这里做了很多假设,并假设.NET绑定与Java绑定大体相似。@Ardesco:是的,我在这里看到了区别。Java的
@FindBys
允许
通过
参数,而我需要在C#
[FindsBy()]
中找到类似的东西。我注意到它允许
CustomFinderType
,但是,在这种情况下,我需要创建自己的类继承
,并且它接受
字符串,这无助于缩短定位器。谢谢mate顺便说一句。从TopPanel.FindElement()和BottomPanel.FindElement()获得的元素是否已修复?您好,欢迎使用堆栈溢出!看看这个,看看你是否能修复你答案中的链接。如果这个链接起作用的话,它将对人们更有用,也更容易阅读。
[FindsBy(How = How.Name, Using = "anElementName", Priority = 0)]
[FindsBy(How = How.Name, Using = "differentElementName", Priority = 1)]
public IWebElement thisElement;