임의 "요소가 더 이상 DOM에 첨부되지 않음"StaleElementReferenceException
나는 그것이 단지 나이기를 바라고 있지만 Selenium Webdriver는 완전한 악몽처럼 보입니다. Chrome 웹 드라이버는 현재 사용할 수 없으며 다른 드라이버는 신뢰할 수 없습니다. 나는 많은 문제와 싸우고 있지만 여기에 하나가 있습니다.
무작위로, 내 테스트는
"org.openqa.selenium.StaleElementReferenceException: Element is no longer attached
to the DOM
System info: os.name: 'Windows 7', os.arch: 'amd64',
os.version: '6.1', java.version: '1.6.0_23'"
웹 드라이버 버전 2.0b3을 사용하고 있습니다. 나는 이것이 FF 및 IE 드라이버에서 발생하는 것을 보았습니다. 내가 이것을 막을 수있는 유일한 방법 Thread.sleep
은 예외가 발생하기 전에 실제 호출을 추가하는 것 입니다. 그것은 좋지 않은 해결 방법이므로 누군가가 내 부분 에서이 모든 것을 더 좋게 만드는 오류를 지적 할 수 있기를 바랍니다.
예, StaleElementReferenceExceptions에 문제가 있으면 테스트가 제대로 작성되지 않았기 때문입니다. 경쟁 조건입니다. 다음 시나리오를 고려하십시오.
WebElement element = driver.findElement(By.id("foo"));
// DOM changes - page is refreshed, or element is removed and re-added
element.click();
이제 요소를 클릭하는 시점에서 요소 참조가 더 이상 유효하지 않습니다. WebDriver가 이러한 상황이 발생할 수있는 모든 경우를 정확하게 추측하는 것은 거의 불가능합니다. 따라서 테스트 / 앱 작성자는 어떤 일이 발생하거나 발생하지 않을 수 있는지 정확하게 알고 있어야합니다. 당신이하고 싶은 일은 DOM이 변경되지 않는 상태에있을 때까지 명시 적으로 기다리는 것입니다. 예를 들어 WebDriverWait를 사용하여 특정 요소가 존재할 때까지 기다립니다.
// times out after 5 seconds
WebDriverWait wait = new WebDriverWait(driver, 5);
// while the following loop runs, the DOM changes -
// page is refreshed, or element is removed and re-added
wait.until(presenceOfElementLocated(By.id("container-element")));
// now we're good - let's click the element
driver.findElement(By.id("foo")).click();
presentOfElementLocated () 메소드는 다음과 같습니다.
private static Function<WebDriver,WebElement> presenceOfElementLocated(final By locator) {
return new Function<WebDriver, WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
return driver.findElement(locator);
}
};
}
현재 Chrome 드라이버가 매우 불안정하다는 것은 옳습니다. Selenium 트렁크에는 Chrome 드라이버가 다시 작성되어 있으며 대부분의 구현은 Chromium 개발자가 트리의 일부로 수행 한 것을 알게되어 기쁩니다.
추신. 또는 위의 예와 같이 명시 적으로 대기하는 대신 암시 적 대기를 활성화 할 수 있습니다. 이렇게하면 WebDriver는 요소가 나타날 때까지 지정된 제한 시간이 초과 될 때까지 항상 루프됩니다.
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS)
내 경험상 명시 적으로 기다리는 것이 항상 더 안정적입니다.
나는 약간의 성공으로 이와 같은 방법을 사용할 수있었습니다.
WebElement getStaleElemById(String id) {
try {
return driver.findElement(By.id(id));
} catch (StaleElementReferenceException e) {
System.out.println("Attempting to recover from StaleElementReferenceException ...");
return getStaleElemById(id);
}
}
예, 더 이상 오래된 것으로 간주되지 않을 때까지 요소를 계속 폴링합니다 (신선한가요?). 실제로 문제의 근원에 도달하지는 않지만 WebDriver 가이 예외를 던지는 데 다소 까다로울 수 있다는 것을 알았습니다. 때로 나는 그것을 얻지 만 때로는 그렇지 않습니다. 또는 DOM이 실제로 변경 될 수 있습니다.
따라서 위의 답변은 필히 시험이 잘못 작성되었음을 나타냅니다. 나는 어떤 식 으로든 상호 작용하지 않은 신선한 페이지에 그것을 가지고 있습니다. DOM을 표현하는 방법이나 WebDriver가 부실하다고 생각하는 부분에는 약간의 결함이 있다고 생각합니다.
AJAX 업데이트가 중간에있을 때 가끔이 오류가 발생합니다. Capybara는 DOM 변경을 기다리는 것에 대해 꽤 똑똑한 것처럼 보였지만 ( wait_until이 Capybara에서 제거 된 이유 참조 ) 기본 대기 시간 2 초는 충분하지 않습니다. _spec_helper.rb_에서 다음과 같이 변경되었습니다.
Capybara.default_max_wait_time = 5
나는 오늘 같은 문제에 직면하고 있으며 요소 참조가 여전히 유효한지 모든 메소드 전에 확인하는 래퍼 클래스를 구성했습니다. 요소를 검색하는 내 솔루션은 매우 간단하므로 공유 할 것이라고 생각했습니다.
private void setElementLocator()
{
this.locatorVariable = "selenium_" + DateTimeMethods.GetTime().ToString();
((IJavaScriptExecutor)this.driver).ExecuteScript(locatorVariable + " = arguments[0];", this.element);
}
private void RetrieveElement()
{
this.element = (IWebElement)((IJavaScriptExecutor)this.driver).ExecuteScript("return " + locatorVariable);
}
전역 js 변수에 요소를 "위치"하거나 저장하고 필요한 경우 요소를 검색하십시오. 페이지가 다시로드되면이 참조는 더 이상 작동하지 않습니다. 그러나 변경이 끝날 때까지만 참조가 유지됩니다. 그리고 대부분의 경우 그 일을해야합니다.
또한 요소를 다시 검색하지 않습니다.
남자
나는 같은 문제가 있었고 내 셀레늄은 오래된 셀레늄 버전으로 인해 발생했습니다. 개발 환경으로 인해 최신 버전으로 업데이트 할 수 없습니다. HTMLUnitWebElement.switchFocusToThisIfNeeded ()에 의해 문제가 발생합니다. 새 페이지를 탐색 할 때 이전 페이지에서 클릭 한 요소가 oldActiveElement
아래에있을 수 있습니다. Selenium은 이전 요소에서 컨텍스트를 가져 오려고하지만 실패합니다. 이것이 향후 릴리스에서 시험판을 구축 한 이유입니다.
셀레늄 -html 단위 드라이버 버전 <2.23.0의 코드 :
private void switchFocusToThisIfNeeded() {
HtmlUnitWebElement oldActiveElement =
((HtmlUnitWebElement)parent.switchTo().activeElement());
boolean jsEnabled = parent.isJavascriptEnabled();
boolean oldActiveEqualsCurrent = oldActiveElement.equals(this);
boolean isBody = oldActiveElement.getTagName().toLowerCase().equals("body");
if (jsEnabled &&
!oldActiveEqualsCurrent &&
!isBody) {
oldActiveElement.element.blur();
element.focus();
}
}
selenium-htmlunit-driver version> = 2.23.0의 코드 :
private void switchFocusToThisIfNeeded() {
HtmlUnitWebElement oldActiveElement =
((HtmlUnitWebElement)parent.switchTo().activeElement());
boolean jsEnabled = parent.isJavascriptEnabled();
boolean oldActiveEqualsCurrent = oldActiveElement.equals(this);
try {
boolean isBody = oldActiveElement.getTagName().toLowerCase().equals("body");
if (jsEnabled &&
!oldActiveEqualsCurrent &&
!isBody) {
oldActiveElement.element.blur();
}
} catch (StaleElementReferenceException ex) {
// old element has gone, do nothing
}
element.focus();
}
2.23.0 이상으로 업데이트하지 않으면 페이지 포커스에 요소를 지정할 수 있습니다. element.click()
예를 들어 방금 사용 했습니다.
검색 입력 상자에 _keys를 보내려고 할 때 방금 입력 한 내용에 따라 자동 업데이트가 수행됩니다. Eero에서 언급했듯이 입력 요소 내부에 텍스트를 입력하는 동안 요소가 Ajax를 업데이트하면이 문제가 발생할 수 있습니다 . 해결책은 한 번에 하나의 문자 를 보내고 입력 요소를 다시 검색하는 것입니다 . (아래 그림은 루비로 표시)
def send_keys_eachchar(webdriver, elem_locator, text_to_send)
text_to_send.each_char do |char|
input_elem = webdriver.find_element(elem_locator)
input_elem.send_keys(char)
end
end
@ jarib의 답변에 추가하기 위해 경쟁 조건을 제거하는 데 도움이되는 몇 가지 확장 방법을 만들었습니다.
내 설정은 다음과 같습니다.
"Driver.cs"라는 클래스가 있습니다. 드라이버 및 기타 유용한 정적 함수에 대한 확장 메소드로 가득 찬 정적 클래스를 포함합니다.
일반적으로 검색해야하는 요소의 경우 다음과 같은 확장 방법을 만듭니다.
public static IWebElement SpecificElementToGet(this IWebDriver driver) {
return driver.FindElement(By.SomeSelector("SelectorText"));
}
이를 통해 코드가있는 테스트 클래스에서 해당 요소를 검색 할 수 있습니다.
driver.SpecificElementToGet();
이제 이것이 결과 StaleElementReferenceException
가되면 드라이버 클래스에 다음 정적 메소드가 있습니다.
public static void WaitForDisplayed(Func<IWebElement> getWebElement, int timeOut)
{
for (int second = 0; ; second++)
{
if (second >= timeOut) Assert.Fail("timeout");
try
{
if (getWebElement().Displayed) break;
}
catch (Exception)
{ }
Thread.Sleep(1000);
}
}
이 함수의 첫 번째 매개 변수는 IWebElement 객체를 반환하는 모든 함수입니다. 두 번째 파라미터는 초 단위의 타임 아웃입니다 (타임 아웃 코드는 Selenium IDE for FireFox에서 복사되었습니다). 이 코드는 다음과 같은 방식으로 오래된 요소 예외를 피하는 데 사용될 수 있습니다.
MyTestDriver.WaitForDisplayed(driver.SpecificElementToGet,5);
위의 코드는 예외가 발생하지 않고 평가 되고 5 초가 지날 driver.SpecificElementToGet().Displayed
때까지 호출 합니다. 5 초 후에 테스트가 실패합니다.driver.SpecificElementToGet()
.Displayed
true
반대로, 요소가 존재하지 않을 때까지 다음 기능을 같은 방식으로 사용할 수 있습니다.
public static void WaitForNotPresent(Func<IWebElement> getWebElement, int timeOut) {
for (int second = 0;; second++) {
if (second >= timeOut) Assert.Fail("timeout");
try
{
if (!getWebElement().Displayed) break;
}
catch (ElementNotVisibleException) { break; }
catch (NoSuchElementException) { break; }
catch (StaleElementReferenceException) { break; }
catch (Exception)
{ }
Thread.Sleep(1000);
}
}
StaleElementReferenceException을 처리하는 편리한 방법을 찾았습니다. 일반적으로 모든 WebElement 메소드에 대해 랩퍼를 작성하여 조치를 재 시도해야하는데, 이는 많은 시간을 낭비하고 낭비합니다.
이 코드 추가
webDriverWait.until((webDriver1) -> (((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete")));
if ((Boolean) ((JavascriptExecutor) webDriver).executeScript("return window.jQuery != undefined")) {
webDriverWait.until((webDriver1) -> (((JavascriptExecutor) webDriver).executeScript("return jQuery.active == 0")));
}
모든 WebElement 작업이 테스트의 안정성을 높일 수 있지만 때때로 StaleElementReferenceException을 얻을 수 있습니다.
그래서 이것은 내가 AspectJ를 사용하여 생각해 낸 것입니다.
package path.to.your.aspects;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebElement;
import org.openqa.selenium.support.pagefactory.DefaultElementLocator;
import org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
@Aspect
public class WebElementAspect {
private static final Logger LOG = LogManager.getLogger(WebElementAspect.class);
/**
* Get your WebDriver instance from some kind of manager
*/
private WebDriver webDriver = DriverManager.getWebDriver();
private WebDriverWait webDriverWait = new WebDriverWait(webDriver, 10);
/**
* This will intercept execution of all methods from WebElement interface
*/
@Pointcut("execution(* org.openqa.selenium.WebElement.*(..))")
public void webElementMethods() {}
/**
* @Around annotation means that you can insert additional logic
* before and after execution of the method
*/
@Around("webElementMethods()")
public Object webElementHandler(ProceedingJoinPoint joinPoint) throws Throwable {
/**
* Waiting until JavaScript and jQuery complete their stuff
*/
waitUntilPageIsLoaded();
/**
* Getting WebElement instance, method, arguments
*/
WebElement webElement = (WebElement) joinPoint.getThis();
Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
Object[] args = joinPoint.getArgs();
/**
* Do some logging if you feel like it
*/
String methodName = method.getName();
if (methodName.contains("click")) {
LOG.info("Clicking on " + getBy(webElement));
} else if (methodName.contains("select")) {
LOG.info("Selecting from " + getBy(webElement));
} else if (methodName.contains("sendKeys")) {
LOG.info("Entering " + args[0].toString() + " into " + getBy(webElement));
}
try {
/**
* Executing WebElement method
*/
return joinPoint.proceed();
} catch (StaleElementReferenceException ex) {
LOG.debug("Intercepted StaleElementReferenceException");
/**
* Refreshing WebElement
* You can use implementation from this blog
* http://www.sahajamit.com/post/mystery-of-stale-element-reference-exception/
* but remove staleness check in the beginning (if(!isElementStale(elem))), because we already caught exception
* and it will result in an endless loop
*/
webElement = StaleElementUtil.refreshElement(webElement);
/**
* Executing method once again on the refreshed WebElement and returning result
*/
return method.invoke(webElement, args);
}
}
private void waitUntilPageIsLoaded() {
webDriverWait.until((webDriver1) -> (((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete")));
if ((Boolean) ((JavascriptExecutor) webDriver).executeScript("return window.jQuery != undefined")) {
webDriverWait.until((webDriver1) -> (((JavascriptExecutor) webDriver).executeScript("return jQuery.active == 0")));
}
}
private static String getBy(WebElement webElement) {
try {
if (webElement instanceof RemoteWebElement) {
try {
Field foundBy = webElement.getClass().getDeclaredField("foundBy");
foundBy.setAccessible(true);
return (String) foundBy.get(webElement);
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
} else {
LocatingElementHandler handler = (LocatingElementHandler) Proxy.getInvocationHandler(webElement);
Field locatorField = handler.getClass().getDeclaredField("locator");
locatorField.setAccessible(true);
DefaultElementLocator locator = (DefaultElementLocator) locatorField.get(handler);
Field byField = locator.getClass().getDeclaredField("by");
byField.setAccessible(true);
return byField.get(locator).toString();
}
} catch (IllegalAccessException | NoSuchFieldException e) {
e.printStackTrace();
}
return null;
}
}
이 측면을 활성화하려면 파일 src\main\resources\META-INF\aop-ajc.xml
을 작성하고 쓰십시오.
<aspectj>
<aspects>
<aspect name="path.to.your.aspects.WebElementAspect"/>
</aspects>
</aspectj>
이것을 당신의 pom.xml
<properties>
<aspectj.version>1.9.1</aspectj.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<configuration>
<argLine>
-javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
</argLine>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
</build>
그리고 그게 전부입니다. 도움이 되길 바랍니다.
In Java 8 you can use very simple method for that:
private Object retryUntilAttached(Supplier<Object> callable) {
try {
return callable.get();
} catch (StaleElementReferenceException e) {
log.warn("\tTrying once again");
return retryUntilAttached(callable);
}
}
FirefoxDriver _driver = new FirefoxDriver();
// create webdriverwait
WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(10));
// create flag/checker
bool result = false;
// wait for the element.
IWebElement elem = wait.Until(x => x.FindElement(By.Id("Element_ID")));
do
{
try
{
// let the driver look for the element again.
elem = _driver.FindElement(By.Id("Element_ID"));
// do your actions.
elem.SendKeys("text");
// it will throw an exception if the element is not in the dom or not
// found but if it didn't, our result will be changed to true.
result = !result;
}
catch (Exception) { }
} while (result != true); // this will continue to look for the element until
// it ends throwing exception.
'Programming' 카테고리의 다른 글
영어 단어 데이터베이스를 얻는 방법? (0) | 2020.06.24 |
---|---|
Apple Appstore에서 특정 앱의 다운로드 수 찾기 (0) | 2020.06.24 |
다른 사용자로 Linux 서비스를 실행하는 모범 사례 (0) | 2020.06.24 |
#pragma는 한 번 C ++ 11 표준의 일부입니까? (0) | 2020.06.24 |
널 입력 가능 ToString () (0) | 2020.06.24 |