2

How can I get the Findby type and string from a WebElement?

I am using a self-built webDriverWait function that will be able to receive By Or Webelement to be used at the presenceOfElementLocated() function.

Defining the WebElement

@FindBy(xpath = "//div[@id='calendar-1234']") 
private WebElement calander;

The Two webDriverWaitFor Functions
The first uses By and is working okay: , and the second uses webElement

public void webDriverWaitFor(WebDriver driver, By by) throws ElementLocatorException {
    try{
        (new WebDriverWait(driver, 5))
        .until(ExpectedConditions.presenceOfElementLocated( by ));
    }
    catch (Exception e) {
        throw new ElementLocatorException(by);
    }

}

The second uses WebElement and I am trying to get the By type and string. this implimintation is not good: By.id(webElement.getAttribute("id"))

public void webDriverWaitFor(WebDriver driver, WebElement webElement) throws ElementLocatorException {
    try{
        (new WebDriverWait(driver, 5))
        .until(ExpectedConditions.presenceOfElementLocated(  By.id(webElement.getAttribute("id")) ));
    }
    catch (Exception e) {
        throw new ElementLocatorException( By.id(webElement.getAttribute("id")) );
    }

}

how will I be able to implement the following?

webDriverWaitFor(driver, calander);
NT3RP
  • 15,262
  • 9
  • 61
  • 97
Shilon
  • 21
  • 2

1 Answers1

0

Usually you could call element.toString() and parse it. The string that is being returned contains all the necessary information.

But it's not going to work in your case as this information can be obtained only after the WebElement has been instantiated. You are using @FindBy tag which means that the element will be looked up at the moment you will try to use it. Your example does not work because the moment you are trying to call webElement.getAttribute the driver.findBy is internally called and it fails since the element does not yet exists.

The only solution I can think of is to write your own wait method

public boolean isElementPresent(WebElement element) {
   try {
      element.isDisplayed();  // we need to call any method on the element in order to force Selenium to look it up
      return true;
   } catch (Exception e) {
      return false;
   }
}

public void webDriverWaitFor(WebElement element) {
   for (int second = 0; ; second++) {
      if (second >= 60) {
         //element not found, log the error
         break;
      }
      if (isElementPresent(element)) {
         //ok, we found the element
         break;
      }
      Thread.sleep(1000);
   }
}

This example will not work well however if you are using implicit waits (each try to call element.isDisplayed would take a lot of time)!

JacekM
  • 4,041
  • 1
  • 27
  • 34
  • How can I write custom `isElementPresent` using pagefactory. Without pagefactory it is written like this: `try{ if (driver.findElements(locator).size() > 0) { return true; } else return false; } catch(Exception e){ return false; }` – paul Jun 11 '16 at 17:34