development

특정 조건을 기다릴 때 WebDriver로 웹 페이지 새로 고침

big-blog 2020. 6. 24. 07:10
반응형

특정 조건을 기다릴 때 WebDriver로 웹 페이지 새로 고침


테스트 중에 웹 페이지를 새로 고치는 더 우아한 방법을 찾고 있습니다 (Selenium2 사용). 방금 F5 키를 보내지 만 드라이버에 전체 웹 페이지를 새로 고치는 방법이 있는지 궁금합니다. 내 코드는 다음과 같습니다.

    while(driver.findElements(By.xpath("//*[text() = 'READY']")).size() == 0 )
        driver.findElement(By.xpath("//body")).sendKeys(Keys.F5);
        //element appear after text READY is presented      
    driver.findElement(By.cssSelector("div.column a")).click();    

수동으로 새로 고친 페이지에서 요소를 찾는 더 나은 솔루션 일 수 있습니다.


Java 또는 JavaScript에서 :

driver.navigate().refresh();

페이지를 새로 고침해야합니다.


파이썬에는 다음과 같은 방법이 있습니다 : driver.refresh(). Java에서는 동일하지 않을 수 있습니다.

또는 driver.get("http://foo.bar");새로 고침 방법이 제대로 작동한다고 생각할 수도 있습니다.


파이썬에서

내장 방법 사용

driver.refresh()

또는 자바 스크립트 실행

driver.execute_script("location.reload()")

Selenium Webdriver를 사용하여 웹 페이지를 새로 고치는 5 가지 방법

특별한 추가 코딩이 없습니다. 방금 기존 기능을 다른 방식으로 사용하여 작동했습니다. 여기 있습니다 :

  1. sendKeys.Keys 메소드 사용

    driver.get("https://accounts.google.com/SignUp");
    driver.findElement(By.id("firstname-placeholder")).sendKeys(Keys.F5);
    
  2. 사용 navigate.refresh을 () 메소드

    driver.get("https://accounts.google.com/SignUp");  
    driver.navigate().refresh();
    
  3. 사용 navigate.to을 () 메소드

    driver.get("https://accounts.google.com/SignUp");  
    driver.navigate().to(driver.getCurrentUrl());
    
  4. 사용 GET을 () 방법

    driver.get("https://accounts.google.com/SignUp");  
    driver.get(driver.getCurrentUrl());
    
  5. 사용 에서 SendKeys를 () 메소드

    driver.get("https://accounts.google.com/SignUp"); 
    driver.findElement(By.id("firstname-placeholder")).sendKeys("\uE035");
    

셀레늄에서 응용 프로그램을 새로 고치는 다양한 방법을 찾았습니다.

1.driver.navigate().refresh();
2.driver.get(driver.getCurrentUrl());
3.driver.navigate().to(driver.getCurrentUrl());
4.driver.findElement(By.id("Contact-us")).sendKeys(Keys.F5); 
5.driver.executeScript("history.go(0)");

라이브 코드는 http://www.ufthelp.com/2014/11/Methods-Browser-Refresh-Selenium.html 링크를 참조하십시오 .


약간 다른 C # 버전은 다음과 같습니다.

driver.Navigate().Refresh();

대체 페이지 새로 고침 (F5)

driver.navigate().refresh();

(또는)

Actions actions = new Actions(driver);
actions.keyDown(Keys.CONTROL).sendKeys(Keys.F5).perform();

주목해야 할 한 가지 중요한 점은 driver.navigate (). refresh () 호출이 때때로 비동기 인 것 같습니다. 즉, 새로 고침이 완료되기를 기다리지 않고 "새로 고침을 시작"하고 추가 실행을 차단하지 않습니다. 브라우저가 페이지를 다시로드하는 동안

이것은 소수의 경우에만 발생하는 것처럼 보이지만 페이지가 실제로 다시로드되기 시작했는지 수동 확인을 추가하여 100 % 작동하는지 확인하는 것이 좋습니다.

기본 페이지 객체 클래스에서 작성한 코드는 다음과 같습니다.

public void reload() {
    // remember reference to current html root element
    final WebElement htmlRoot = getDriver().findElement(By.tagName("html"));

    // the refresh seems to sometimes be asynchronous, so this sometimes just kicks off the refresh,
    // but doesn't actually wait for the fresh to finish
    getDriver().navigate().refresh();

    // verify page started reloading by checking that the html root is not present anymore
    final long startTime = System.currentTimeMillis();
    final long maxLoadTime = TimeUnit.SECONDS.toMillis(getMaximumLoadTime());
    boolean startedReloading = false;
    do {
        try {
            startedReloading = !htmlRoot.isDisplayed();
        } catch (ElementNotVisibleException | StaleElementReferenceException ex) {
            startedReloading = true;
        }
    } while (!startedReloading && (System.currentTimeMillis() - startTime < maxLoadTime));

    if (!startedReloading) {
        throw new IllegalStateException("Page " + getName() + " did not start reloading in " + maxLoadTime + "ms");
    }

    // verify page finished reloading
    verify();
}

몇 가지 참고 사항 :

  • 페이지를 새로 고침하기 때문에 다시로드가 시작되기 전과 완료된 후에 요소가 있기 때문에 주어진 요소가 있는지 확인할 수 없습니다. 따라서 때로는 사실이 될 수도 있지만 페이지가 아직로드되지 않았습니다.
  • When the page reloads, checking WebElement.isDisplayed() will throw a StaleElementReferenceException. The rest is just to cover all bases
  • getName(): internal method that gets the name of the page
  • getMaximumLoadTime(): internal method that returns how long page should be allowed to load in seconds
  • verify(): internal method makes sure the page actually loaded

Again, in a vast majority of cases, the do/while loop runs a single time because the code beyond navigate().refresh() doesn't get executed until the browser actually reloaded the page completely, but we've seen cases where it actually takes seconds to get through that loop because the navigate().refresh() didn't block until the browser finished loading.


You can also try

Driver.Instance.Navigate().Refresh();

There is also a case when you want to refresh only specific iframe on page and not the full page.

You do is as follows:

public void refreshIFrameByJavaScriptExecutor(String iFrameId){
        String script= "document.getElementById('" + iFrameId+ "').src = " + "document.getElementById('" + iFrameId+ "').src";
       ((IJavaScriptExecutor)WebDriver).ExecuteScript(script);
}

In PHP:

$driver->navigate()->refresh();

Another way to refresh the current page using selenium in Java.

//first: get the current URL in a String variable
String currentURL = driver.getCurrentUrl(); 
//second: call the current URL
driver.get(currentURL); 

Using this will refresh the current page like clicking the address bar of the browser and pressing enter.

참고URL : https://stackoverflow.com/questions/10245641/refreshing-web-page-by-webdriver-when-waiting-for-specific-condition

반응형