IT박스

WebDriver-Java를 사용하여 요소 대기

itboxs 2020. 11. 1. 17:28

WebDriver-Java를 사용하여 요소 대기


waitForElementPresent클릭하기 전에 요소가 표시되는지 확인 하기 위해 비슷한 것을 찾고 있습니다. 에서이 작업을 수행 할 수 있다고 생각 implicitWait했기 때문에 다음을 사용했습니다.

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

다음 클릭

driver.findElement(By.id(prop.getProperty(vName))).click();

불행히도 때로는 요소를 기다리고 때로는 그렇지 않습니다. 나는 잠시 동안이 해결책을 찾았습니다.

for (int second = 0;; second++) {
            Thread.sleep(sleepTime);
            if (second >= 10)
                fail("timeout : " + vName);
            try {
                if (driver.findElement(By.id(prop.getProperty(vName)))
                        .isDisplayed())
                    break;
            } catch (Exception e) {
                writeToExcel("data.xls", e.toString(),
                        parameters.currentTestRow, 46);
            }
        }
        driver.findElement(By.id(prop.getProperty(vName))).click();

그리고 괜찮 았지만 시간이 초과되기 전에 10 번 5, 50 초를 기다려야했습니다. 조금 많이. 그래서 암묵적으로 대기를 1 초로 설정했고 지금까지 모두 괜찮아 보였습니다. 이제 어떤 것들은 시간 초과 전에 10 초를 기다리지 만 다른 것들은 1 초 후에 시간 초과되기 때문입니다.

코드에 존재 / 표시되는 요소를 기다리는 것을 어떻게 처리합니까? 어떤 힌트라도 괜찮습니다.


이것이 내 코드에서 수행하는 방법입니다.

WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>));

또는

wait.until(ExpectedConditions.elementToBeClickable(By.id<locator>));

정확합니다.

또한보십시오:


명시 적 대기 또는 유창 대기를 사용할 수 있습니다.

명시 적 대기의 예-

WebDriverWait wait = new WebDriverWait(WebDriverRefrence,20);
WebElement aboutMe;
aboutMe= wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("about_me")));     

유창한 대기의 예-

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)                            
.withTimeout(20, TimeUnit.SECONDS)          
.pollingEvery(5, TimeUnit.SECONDS)          
.ignoring(NoSuchElementException.class);    

  WebElement aboutMe= wait.until(new Function<WebDriver, WebElement>() {       
public WebElement apply(WebDriver driver) { 
return driver.findElement(By.id("about_me"));     
 }  
});  

자세한 내용은 튜토리얼확인 하십시오.


.NET과 함께 많은 경쟁 조건이 elementToBeClickable있습니다. https://github.com/angular/protractor/issues/2313을 참조 하십시오 . 약간의 무자비한 힘이 있어도이 라인을 따라 어떤 것이 합리적으로 잘 작동했습니다.

Awaitility.await()
        .atMost(timeout)
        .ignoreException(NoSuchElementException.class)
        .ignoreExceptionsMatching(
            Matchers.allOf(
                Matchers.instanceOf(WebDriverException.class),
                Matchers.hasProperty(
                    "message",
                    Matchers.containsString("is not clickable at point")
                )
            )
        ).until(
            () -> {
                this.driver.findElement(locator).click();
                return true;
            },
            Matchers.is(true)
        );

위의 wait 문은 Explicit wait의 좋은 예입니다.

명시 적 대기는 특정 웹 요소에 국한된 지능형 대기입니다 (위의 x-path에서 언급 됨).

By Using explicit waits you are basically telling WebDriver at the max it is to wait for X units(whatever you have given as timeoutInSeconds) of time before it gives up.

참고URL : https://stackoverflow.com/questions/11736027/webdriver-wait-for-element-using-java