Selenium InternetExplorerDriver StaleElementReferenceException

Selenium InternetExplorerDriver StaleElementReferenceException,selenium,webdriver,Selenium,Webdriver,我有以下Selenium测试代码: waitForElementList(driver, timeout, By.xpath(elementA); waitForElementList(driver, timeout, By.xpath(elementB); waitForElementList(driver, timeout, By.xpath(elementC); waitForElementList(driver, timeout, By.xpath(elementD); waitForE

我有以下Selenium测试代码:

waitForElementList(driver, timeout, By.xpath(elementA);
waitForElementList(driver, timeout, By.xpath(elementB);
waitForElementList(driver, timeout, By.xpath(elementC);
waitForElementList(driver, timeout, By.xpath(elementD);
waitForElementList(driver, timeout, By.xpath(elementE);
waitForElementList(driver, timeout, By.xpath(elementF);

List<WebElement> elementA = driver.findElements(By.xpath(elementA));
List<WebElement> elementB = driver.findElements(By.xpath(elementB));
List<WebElement> elementC = driver.findElements(By.xpath(elementC));
List<WebElement> elementD = driver.findElements(By.xpath(elementD));
List<WebElement> elementE = driver.findElements(By.xpath(elementE));
List<WebElement> elementF = driver.findElements(By.xpath(elementF));

for(int i = 0; i < elementA.size(); i++){
    int id = Integer.valueOf(elementA.get(i).getAttribute("class"));
    //doing things with the other elements
}

public static void waitForElementList(WebDriver driver, int timeout, final By locator){
    FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver)
       .withTimeout(timeout, TimeUnit.SECONDS)
       .pollingEvery(1, TimeUnit.SECONDS)
       .ignoring(NoSuchElementException.class)
       .ignoring(StaleElementReferenceException.class);

    wait.until(new Function<WebDriver, List<WebElement>>() {
        public List<WebElement> apply(WebDriver driver) {
            return driver.findElements(locator);
        }
    });
}
waitForElementList(驱动程序,超时,By.xpath(elementA));
waitForElementList(驱动程序、超时、By.xpath(elementB));
waitForElementList(驱动程序,超时,By.xpath(elementC));
waitForElementList(驱动程序,超时,By.xpath(elementD));
waitForElementList(驱动程序,超时,By.xpath(elementE));
waitForElementList(驱动程序,超时,By.xpath(elementF));
List elementA=driver.findElements(By.xpath(elementA));
List elementB=driver.findElements(By.xpath(elementB));
List elementC=driver.findElements(By.xpath(elementC));
List elementD=driver.findElements(By.xpath(elementD));
List elementE=driver.findElements(By.xpath(elementE));
List elementF=driver.findElements(By.xpath(elementF));
对于(int i=0;i
如果我在Internet Explorer以外的任何浏览器中运行这段代码,它都会正常工作。但是如果我在Internet Explorer中运行它,它有时会在它想要获取属性“class”的行上崩溃。出现StaleElementReferenceException。有趣的是,此异常只出现在Windows 7 IE9上,而不出现在Windows XP IE8上。
我在谷歌上搜索了很多如何解决这个问题,但没有任何解决方案对我有帮助。如果某个元素已刷新或介于waitForElementList()方法和getAttribute调用之间,则会出现此异常。如果我只运行第一个waitForElementList()方法(对于elementA),而不运行另一个waitForElementList()方法,则会出现此异常-方法。我不知道现在该怎么办。谢谢你们的帮助。

刷新可以通过页面上的javascript代码触发。 它为一些IE专用的页面内容留出了空间,而这些内容正困扰着你

我将重新安排代码,尽快提取元素的属性,作为临时甚至永久的解决方案。看起来您已经知道如何做到这一点


UPD:

如果您想不出任何帮助,请使用第三方库提取实际属性值,并使用selenium进行单击和浏览

下面是在python中同时使用
selenium
lxml
的示例:

from lxml import etree
from selenium.webdriver import Firefox  # IE, or Chrome


SEARCH_EDITBOX_CSS_CLASS_XPATH = '//input[@id="lst-ib"]/@class'

browser = Firefox()
browser.get('http://google.com')
html = browser.page_source

tree = etree.fromstring(html, parser=etree.HTMLParser())
css_classes = tree.xpath(SEARCH_EDITBOX_CSS_CLASS_XPATH)

print css_classes
# prints out list of 'class' attributes for each of elements found:
# ['gsfi']

一旦您将整个页面的源代码放入变量中,站点脚本将不会更改它,您将有无限的时间来使用它。

页面上的javascript代码可以触发刷新。 它为一些IE专用的页面内容留出了空间,而这些内容正困扰着你

我将重新安排代码,尽快提取元素的属性,作为临时甚至永久的解决方案。看起来您已经知道如何做到这一点


UPD:

如果您想不出任何帮助,请使用第三方库提取实际属性值,并使用selenium进行单击和浏览

下面是在python中同时使用
selenium
lxml
的示例:

from lxml import etree
from selenium.webdriver import Firefox  # IE, or Chrome


SEARCH_EDITBOX_CSS_CLASS_XPATH = '//input[@id="lst-ib"]/@class'

browser = Firefox()
browser.get('http://google.com')
html = browser.page_source

tree = etree.fromstring(html, parser=etree.HTMLParser())
css_classes = tree.xpath(SEARCH_EDITBOX_CSS_CLASS_XPATH)

print css_classes
# prints out list of 'class' attributes for each of elements found:
# ['gsfi']

一旦您将整个页面的源代码放入变量中,站点脚本将不会更改它,您将有无限的时间来使用它。

事实上,这在一开始是有帮助的,但StaleElementReferenceException仍然不时出现。这使得为IE编写测试几乎不可能。我的意思是,您有两行代码:第一个是“findElement(…)”,第二个类似于“getText”、“getAttribute”或者随便什么。在这两行代码之间,Selenium告诉我元素不再有效?我的意思是,这是毫秒的时间,怎么可能?这真的使得用IE编写稳定的测试变得不可能。对这个问题有任何进一步的帮助吗?太好了,非常感谢!@user1169166,我已经更新了答案,使其保持一致呃,谢谢你的回答。我会尝试一下。但是仍然没有真正的解决方案吗?在后台没有java脚本在后台操纵DOM,所以我不明白为什么Selenium告诉我WebElement不再有效。它确实有效。这是我不明白的。实际上这一点一开始很有帮助,但是aleElementReferenceException仍然不时出现。这使得为IE编写测试几乎不可能。我的意思是,您有两行代码:第一行是“findElement(…)”,第二行类似于“getText”、“getAttribute”或者随便什么。在这两行代码之间,Selenium告诉我元素不再有效?我的意思是,这是毫秒的时间,怎么可能?这真的使得用IE编写稳定的测试变得不可能。对这个问题有任何进一步的帮助吗?太好了,非常感谢!@user1169166,我已经更新了答案,使其保持一致呃,谢谢你的回答。我会尝试一下。但是仍然没有真正的解决方案吗?后台没有java脚本在后台操纵DOM,所以我不明白为什么Selenium告诉我WebElement不再有效。它确实有效。这就是我不明白的。