programing

Selenium - 페이지가 완전히 로드될 때까지 기다리는 방법

minecode 2023. 1. 14. 09:59
반응형

Selenium - 페이지가 완전히 로드될 때까지 기다리는 방법

Java와 Selenium WebDriver를 사용하여 테스트 케이스를 자동화하려고 합니다.다음과 같은 시나리오가 있습니다.

  • 'Products'라는 페이지가 있습니다.Product 페이지에서 View Details 링크를 클릭하면 해당 항목의 세부 정보를 포함하는 팝업(모달 대화 상자)이 나타납니다.
  • 팝업에서 '닫기' 버튼을 클릭하면 팝업이 닫히고 페이지가 자동으로 새로 고쳐집니다(페이지가 새로고침 중이고 내용은 변경되지 않습니다).
  • 팝업을 닫은 후 같은 페이지의 'Add Item' 버튼을 클릭해야 합니다.그러나 WebDriver가 'Add Item' 버튼을 찾으려 할 때 인터넷 속도가 너무 빠르면 WebDriver가 요소를 찾아 클릭할 수 있습니다.

  • , 새로 고침 전에 되고 WebDriver가 웹드라이버로 바뀝니다.StaleElementReferenceException어나나다

  • 후나 에 모든 됩니다.StaleElementReferenceException어나나다

는 잘 합니다.Thread.sleep(3000);[항목 추가] 항목 추가이 문제에 대한 다른 해결 방법이 있습니까?

3개의 답변을 조합할 수 있습니다.

  1. 웹 드라이버 인스턴스를 작성한 후 즉시 암묵적인 대기 시간을 설정합니다.

    _ = driver.Manage().Timeouts().ImplicitWait;

    이렇게 하면 페이지 탐색 또는 페이지 새로고침 시마다 페이지가 완전히 로드될 때까지 대기합니다.

  2. 네비게이션 후 JavaScript를 합니다.return document.readyState"complete"이 반환됩니다.JavaScript를 사용합니다.플플: :

    C#

    new WebDriverWait(driver, MyDefaultTimeout).Until(
    d => ((IJavaScriptExecutor) d).ExecuteScript("return document.readyState").Equals("complete"));
    

    자바

    new WebDriverWait(firefoxDriver, pageLoadTimeout).until(
          webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete"));
    
  3. URL 이 예상 패턴과 일치하는지 확인합니다.

추가 버튼을 클릭하기 전에 페이지가 새로고침될 때까지 기다려야 할 것 같습니다.이 경우 새로고침된 요소를 클릭하기 전에 "Add Item" 요소가 오래될 때까지 기다릴 수 있습니다.

WebDriverWait wait = new WebDriverWait(driver, 20);
By addItem = By.xpath("//input[.='Add Item']");

// get the "Add Item" element
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(addItem));

//trigger the reaload of the page
driver.findElement(By.id("...")).click();

// wait the element "Add Item" to become stale
wait.until(ExpectedConditions.stalenessOf(element));

// click on "Add Item" once the page is reloaded
wait.until(ExpectedConditions.presenceOfElementLocated(addItem)).click();

추가 항목을 클릭하기 전에 여러 가지 방법으로 이 작업을 수행할 수 있습니다.

WebDriverWait wait = new WebDriverWait(driver, 40);
wait.until(ExpectedConditions.elementToBeClickable(By.id("urelementid"))); // instead of id you can use cssSelector or xpath of your element.

또는 다음과 같이 입력합니다.

wait.until(ExpectedConditions.visibilityOfElementLocated("urelement"));

이렇게 기다리셔도 됩니다.이전 페이지 요소가 표시되지 않을 때까지 기다리는 경우:

wait.until(ExpectedConditions.invisibilityOfElementLocated("urelement"));

다음 링크에서 사용할 수 있는 모든 Selenium WebDriver API를 찾을 수 있습니다.wait및 그 문서.

예, (시나리오를 사용하여) 먼저 "Add Item"을 클릭하기 위한 로케이터 전략을 정의한 후 팝업을 닫으면 페이지가 새로 고쳐집니다. 따라서 "Add Item"에 정의된 참조가 메모리에 손실되므로 이를 극복하기 위해 "Add Item"에 대한 로케이터 전략을 다시 정의해야 합니다.

그것을 더미 코드로 이해하다.

// clicking on view details 
driver.findElement(By.id("")).click();
// closing the pop up 
driver.findElement(By.id("")).click();


// and when you try to click on Add Item
driver.findElement(By.id("")).click();
// you get stale element exception as reference to add item is lost 
// so to overcome this you have to re identify the locator strategy for add item 
// Please note : this is one of the way to overcome stale element exception 

// Step 1 please add a universal wait in your script like below 
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); // just after you have initiated browser

가장 일반적으로 사용되는 셀레늄 지연에는 두 가지 다른 방법이 있습니다.다음을 시도해 보십시오.

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

두 번째는 단순히 그 방법을 사용하여 캐치 방법을 시도하면 원하는 결과를 얻을 수 있습니다.예제 코드를 원하시면 언제든지 문의해 주십시오.관련 코드를 알려드리겠습니다.

언급URL : https://stackoverflow.com/questions/36590274/selenium-how-to-wait-until-page-is-completely-loaded

반응형