Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to find a WebElement by another WebElement using Page Factory
    text
    copied!<p>Had a brief scan at the source code, not sure what's the best practice for this.</p> <p>For example, say I have a page object 'DummyPage', which has two panel elements <code>TopPanel</code> and <code>BottomPanel</code>. Each of the panel has some elements, which are found by <code>TopPanel.FindElement()</code> instead of <code>driver.FindElement()</code>. How to apply Page Factory for this?</p> <p>I'm aware of that <code>PageFactory.InitElements(ISearchContext, Object)</code> takes in <code>ISearchContext</code>, however, I'm not sure how can I use it for the page and the panel elements in one class.</p> <pre class="lang-c# prettyprint-override"><code>public class DummyPage { private IWebDriver driver; public DummyPage(IWebDriver driver) { this.driver = driver; } public IList&lt;IWebElement&gt; DummyLinks { get { return driver.FindElements(By.CssSelector(".some-dummy-links")); } } public IWebElement TopPanel { get { return driver.FindElement(By.Id("top-panel")); } } public IWebElement BottomPanel { get { return driver.FindElement(By.Id("bottom-panel")); } } public IWebElement FooInTopPanel { get { return TopPanel.FindElement(By.CssSelector(".something")); } } public IWebElement FooInBottomPanel { get { return BottomPanel.FindElement(By.CssSelector(".something")); } } } public class DummyPageWithPageFactory { public DummyPageWithPageFactory(IWebDriver driver) { PageFactory.InitElements(driver, this); } [FindsBy(How = How.CssSelector, Using = ".some-dummy-links")] public IList&lt;IWebElement&gt; DummyLinks { get; private set; } [FindsBy(How = How.Id, Using = "top-panel")] public IWebElement TopPanel { get; private set; } [FindsBy(How = How.Id, Using = "bottom-panel")] public IWebElement BottomPanel { get; private set; } //public IWebElement FooInTopPanel { get; private set; } //public IWebElement FooInBottomPanel { get; private set; } } </code></pre> <p>If I use <code>driver.FindElement()</code> for all instances and concatenate all locators, I might be facing another situation, that all locators are too long and I can't use variables within the C# attributes.</p> <pre><code>[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")] </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload