2013-05-29 2 views
2

WebElement에서 Findby 유형 및 문자열을 얻으려면 어떻게해야합니까?WebElement에서 Findby 유형 및 문자열을 가져 오는 방법

나는 presenceOfElementLocated() 함수에서 사용할 Web 요소를받을 수있는 자체 내장 webDriverWait 함수를 사용하고 있습니다. WebElement 정의

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

첫 번째는 작성자 사용하고 좋아하고있다
기능 설명 webDriverWaitFor 두 : 두 번째 용도는 두 번째 사용

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); 
    } 

} 

webElement WebElement By 유형 및 문자열을 가져 오려고합니다. 이 implimintation 잘되지 않습니다 : 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"))); 
    } 

} 
나는 다음과 같은 구현할 수있을 것입니다 방법

?

webDriverWaitFor(driver, calander); 

답변

0

보통은 element.toString()를 호출하고 분석 할 수있다. 반환되는 문자열에는 필요한 모든 정보가 들어 있습니다.

그러나이 정보는 WebElement가 인스턴스화 된 후에 만 ​​얻을 수 있으므로 사용자의 경우에는 작동하지 않습니다. @FindBy 태그를 사용하고 있습니다. 즉, 요소를 사용하려고 할 때 요소가 조회됩니다. webElement.getAttribute를 호출하려고 할 때 driver.findBy가 내부적으로 호출되어 요소가 아직 없으므로 예제가 작동하지 않으므로 예제가 작동하지 않습니다. 내가 생각할 수있는

유일한 해결책은 당신이 암시 대기를 사용하는 경우이 예제는 잘하지만 작동하지 않습니다

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); 
    } 
} 

가 (각각이 많이 걸릴 것 element.isDisplayed를 호출하려고 자신의 대기 방법을 작성하는 것입니다 시간의)!

+0

pagefactory를 사용하여 어떻게 custom isElementPresent를 쓸 수 있습니까? pagefactory 없이는는 다음과 같이 기록됩니다 는'{ \t \t \t 경우 (driver.findElements (로케이터) 크기는()> 0) { \t \t \t \t 반환 사실을 시도; \t \t \t} else else \t \t \t \t false; \t \t} \t \t 캐치 (예외 전자) {거짓 \t \t \t 반환; \t \t} – paul

관련 문제