1

I'm using the PageObjectModel as I'm understanding it. The FindsBy elements are instantiated by the PageFactory (in the constructor)

public class wizard_Uittreksel_Bestemmeling
{
    private readonly IWebDriver _driver;

    [FindsBy(How = How.CssSelector, Using = "input[name=\"OntvangstTypeCoid\"][value=\"Permail\"]")]
    private IWebElement radio_ontvangenPerMail;
    [FindsBy(How = How.CssSelector, Using = "input[name=\"OntvangstTypeCoid\"][value=\"Perbrief\"]")]
    private IWebElement radio_ontvangenPerBrief;
    [FindsBy(How = How.Id, Using = "Email")]
    private IWebElement txt_email;

Now, this is working fine. However, for these elements I sometimes need

  1. their XPath, e.g. for finding dynamic elements in the same containing DIV
  2. their ID or CssSelector, e.g. to determine if the element exists, using driver.FindElement(By.Locator())

As there's no way to extract these locator values from the IWebElement, I wonder if anyone else has had this issue? What would be the cleanest way to solve this issue?

2
  • Are you sure you need to extract these locator values? If you need to find a dynamic element in the same containing DIV, you can declare the DIV itself as a WebElement and use the WebElement's findElement() method. If you need to determine if an element exists, just call the WebElement's isDisplayed() method. Oct 14 '14 at 13:04
  • I think what you're trying to do goes against the page object pattern, at least as I understand it. Locating controls in your tests is not something you want to do if you're trying to use page object. I'm not certain how you would accomplish #1 in Java, but for #2 you should be able to use IWebElement.Displayed Oct 14 '14 at 16:57
0
  1. I'm using (when it is needs) mix of [FindsBy] and "complex" elements (defined as such "dummy example")

    protected string someVariable= "someXPath_part";

    someVariable + "[" + number.ToString() + "]"

  2. As beekeeper said, driver.FindElement(By.Locator()) and locator, defined by [FindsBy] are equial, so if you need to check existing/enabling of element, just use

    txt_email.Displayed;

    txt_email.Enabled;

1
  • 1
    For 1. that's probably what I'll do. Makes the XPaths reusable too, if need be. For 2., I've written the method: public static bool isElementPresent(IWebDriver d, IWebElement el) with try { bool test = el.Displayed; } It feels strange just to call a random property, but hey, it works. Thanks for the input.
    – FDM
    Oct 15 '14 at 5:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.