Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java StaleElementReferenceException:确定导致异常的元素_Java_Selenium - Fatal编程技术网

Java StaleElementReferenceException:确定导致异常的元素

Java StaleElementReferenceException:确定导致异常的元素,java,selenium,Java,Selenium,我已经做了一些非常彻底的挖掘,并且很难确定到底是哪个元素导致了这个异常被抛出。我正在使用Java和Selenium 2,最近接管了一个相当大的测试套件来维护。我不想盲目地在失败的代码行中或周围放置fluent等待,而是想实际确定是哪些元素导致了问题 我曾经考虑过将一堆单行逻辑分解成更多行代码来帮助解决问题,但由于这些随机故障是间歇性的,而且到处都是,所以我试图找到一种方法来获取堆栈跟踪中元素或定位器的实际名称 这是可能的还是我必须在代码端进行一系列重构 下面是一个堆栈跟踪示例: Starting

我已经做了一些非常彻底的挖掘,并且很难确定到底是哪个元素导致了这个异常被抛出。我正在使用Java和Selenium 2,最近接管了一个相当大的测试套件来维护。我不想盲目地在失败的代码行中或周围放置fluent等待,而是想实际确定是哪些元素导致了问题

我曾经考虑过将一堆单行逻辑分解成更多行代码来帮助解决问题,但由于这些随机故障是间歇性的,而且到处都是,所以我试图找到一种方法来获取堆栈跟踪中元素或定位器的实际名称

这是可能的还是我必须在代码端进行一系列重构

下面是一个堆栈跟踪示例:

Starting ChromeDriver (v2.3) on port 24902
org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
  (Session info: chrome=30.0.1599.101)
  (Driver info: chromedriver=2.3,platform=Mac OS X 10.8.5 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 5 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.33.0', revision: '4ecaf82108b2a6cc6f006aae81961236eba93358', time: '2013-05-22 12:00:17'
System info: os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.8.5', java.version: '1.7.0_21'
Session ID: f3e5d2c3eb54afc2fcaacc1c663435e9
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{platform=MAC, acceptSslCerts=true, javascriptEnabled=true, browserName=chrome, chrome={chromedriverVersion=2.3}, rotatable=false, locationContextEnabled=true, version=30.0.1599.101, cssSelectorsEnabled=true, databaseEnabled=true, handlesAlerts=true, browserConnectionEnabled=false, webStorageEnabled=true, nativeEvents=true, applicationCacheEnabled=false, takesScreenshot=true}]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:191)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268)
    at org.openqa.selenium.remote.RemoteWebElement.findElements(RemoteWebElement.java:187)
    at org.openqa.selenium.remote.RemoteWebElement.findElementsByCssSelector(RemoteWebElement.java:240)
    at org.openqa.selenium.By$ByCssSelector.findElements(By.java:417)
    at org.openqa.selenium.remote.RemoteWebElement.findElements(RemoteWebElement.java:163)
    at com.xmatters.webui.pages.home.developer.managerelevanceengines.formdetails.layoutsections.CustomSectionScreen.rowPropertyIsInCustomSection(CustomSectionScreen.java:54)
    at com.xmatters.webui.pages.home.developer.managerelevanceengines.formdetails.layoutsections.CustomSectionScreen.editNameForProperty(CustomSectionScreen.java:90)
    at com.xmatters.webdriver.tests.REBProperties_BooleanTest.confirmBooleanPropertyNameCanBeEditedOnLayoutTab(REBProperties_BooleanTest.java:526)
可疑行上的代码在此帮助函数中。 第54行是包含
List…

public Integer rowPropertyIsInCustomSection(String propertyName) {

        wait.until(ExpectedConditions.presenceOfElementLocated(customSectionLocator));
        WebElement customSection = driver.findElement(customSectionLocator);
        //wait until properties are present in custom section

        wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(propertiesInCustomSectionLocator));
        List<WebElement> propertiesInCustomSection = customSection.findElements(propertyNameLocatorInCustomSection);

        By propertyNameLoc = By.id("propertyName");
        for (int i = 0; i < propertiesInCustomSection.size(); i++) {
            wait.until(ExpectedConditions.visibilityOfElementLocated(propertyNameLoc));
            String propName = propertiesInCustomSection.get(i).findElement(propertyNameLoc).getText();
            if (propName.equals(propertyName)) {
                return i;
            }
        }
        return null;
public Integer行属性IsIncosTomSection(字符串属性名称){
等待。直到(预期条件。元素存在已定位(customSectionLocator));
WebElement customSection=driver.findElement(customSectionLocator);
//等待属性出现在自定义节中
等待.直到(由(propertiesInCustomSectionLocator)慢配的Allelements的预期条件.存在);
List propertiesInCustomSection=customSection.findElements(propertyNameLocatorInCustomSection);
By propertyName=By.id(“propertyName”);
对于(int i=0;i
如果其他地方已经回答了,我很抱歉,但我很难确定这一点,因为堆栈跟踪中没有什么可处理的。这段代码都是由其他人编写的,所以逐段剖析它是我目前的最后手段

提前感谢您的指点


达尔文。

我很确定这是
customSection
元素

WebElement#findElements()
绝对不会在任何新找到的元素上抛出
StaleElementReferenceException
(这将是一个糟糕的错误)

如中所述,它将抛出在以前找到的
WebElement
上调用的大多数方法。因此,当您尝试在元素上调用
findElements()
时,它会执行新鲜度检查,但由于某种原因失败


在搜索
customSection
之前,请尝试等待它的一些属性出现。在向该节添加属性时,该元素可能会被丢弃并替换为一个新的元素。或者您可以尝试所描述的一种技术,即
NeverStaleWebElement

我很确定它就是
自定义节
元素

WebElement#findElements()
绝对不会在任何新找到的元素上抛出
StaleElementReferenceException
(这将是一个糟糕的错误)

如中所述,它将抛出在以前找到的
WebElement
上调用的大多数方法。因此,当您尝试在元素上调用
findElements()
时,它会执行新鲜度检查,但由于某种原因失败


在搜索
customSection
之前,请尝试等待其某些属性显示。在向该节添加属性时,该元素可能会被丢弃并替换为新的元素。或者您可以尝试所述的一种技术,即
NeverStaleWebElement

是的,似乎
是tomSection
以及其他几个元素由于多次刷新页面而受到影响。换句话说,一个元素在加载页面时可能会过时两次。我查看了NeverStaleWebElement示例,但我担心我会陷入无限循环或竞争条件的情况(如果添加隐含时间后退出,页面加载缓慢).至于堆栈跟踪本身,我假设不可能获得报告的过时元素的名称?@DarwinAllen,不幸的是,不可能。尽管可以通过一些黑客代码获得用于查找元素的“By”对象。是的,
customSection
以及其他几个元素nts由于多次刷新页面而受到影响。换句话说,一个元素在加载页面时可能会过时两次。我查看了NeverStaleWebElement示例,但我担心我会陷入无限循环或竞争状态(如果添加隐含时间后退出,页面加载缓慢).至于堆栈跟踪本身,我假设不可能获得报告的过时元素的名称?@DarwinAllen,不幸的是,不可能。尽管可以通过一些黑客代码获得用于查找元素的“By”对象。