当前位置: 首页 > 知识库问答 >
问题:

GWT的Selenium WebDriver过时元素引用异常

张建树
2023-03-14

好的,我阅读了所有其他的链接,我尝试了上述不同解决方案的变体,但是它们都不适合我。

我的问题是,我有以下代码

package com.autotest.test.css;


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;

import com.google.common.base.Predicate;

import java.util.concurrent.TimeUnit;

import cucumber.annotation.*;
import cucumber.annotation.en.*;
import static org.junit.Assert.assertEquals;


public class SaleStepsPre {

    private WebDriver driver;
    private String baseUrl;


    @Before
    public void setUp() {
    System.setProperty("webdriver.chrome.driver", "/Users/AppData/Local/Google/Chrome/Application/chromedriver.exe");   
    driver = new ChromeDriver();

    baseUrl = "http://xxxxx";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Given("^I navigate to the css application$")
    public void I_navigate_to_the_css_application() {
    driver.get(baseUrl + "/care/a#brochureware-home");
    }

    @When("^I select the prepaid catalog$")
    public void I_select_the_prepaid_catalog() {
    driver.findElement(By.xpath("//div[@id='brochureware-home']/div/div/div/div[2]/div[2]/div/div")).click();
    }

    @When("^I select the add to basket for product$")
    public void I_select_the_add_to_basket_for_product() {
    driver.findElement(By.xpath("//*[@id='salesItem']/div[1]/div[1]/div/div[5]/div[1]/button")).click();
    }

    @When("^then I Click on the basket icon to go to basket$")
    public void then_I_Click_on_the_basket_icon_to_go_to_basket()  {
        // times out after 5 seconds
        // while the following loop runs, the DOM changes - 
        // page is refreshed, or element is removed and re-added
        //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
        //WebElement searchBox;
        //searchBox = driver.findElement(By.xpath("//input[@type='text']"));
    //driver.findElement(By.xpath("html/body/div[2]/div[1]/nav/div[1]/div[3]/div[2]/div/ul[1]/li[5]/a/img")).click();
    driver.findElement(By.cssSelector("c-menuimage")).click();
    }

    //@When("^then I click on the checkout button$")
    //public void then_I_click_on_the_checkout_button() {
    //driver.findElement(By.xpath("(//button[@type='button'])[9]")).click();
    //}


    @Then("^show product y$")
    public void show_product_y()  {

}


}

然而,我得到了以下错误:

For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
      B
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html

这是篮子图标的css路径,它在菜单上。

body > div:nth-child(3) > div:nth-child(1) > nav > div.container-fluid.c-wide > div.c-kill > div.collapse.navbar-collapse.c-2ndmenu > div > ul.nav.navbar-nav.navbar-left > li:nth-child(5) > a > img

网站是GWT,步骤如下:1。点击物品添加到购物篮2。增加到第三篮。点击篮子进入篮子。

然而,我似乎无法做到这一点。

共有1个答案

於功
2023-03-14

我遇到了一些问题,隐式等待只适用于真正的页面重新加载,当页面被动态重新加载(比如ajax)时,这将失败。

您可以尝试等待项目的预期条件,它们很好,易于使用,而且健壮。您可以将它们配置为忽略某些异常,这样您就可以尝试在给定的时间内定位一个元素,然后失败。这甚至适用于ajax

NosElement(在我的案例中我忽略了一个小的异常):

protected <T> T waitForPageToLoad(ExpectedCondition<T> condition,   String errorMessage) {   
    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(MAX_WAITING_TIME, SECONDS).ignoring(StaleElementReferenceException.class).ignoring(NoSuchElementException.class).pollingEvery(100, MILLISECONDS).withMessage(errorMessage);
    T result = wait.until(condition);
    return result;
}

MAX_WAITING_TIME是这个方法抛出异常的时间

然后,您可以将其用于如下预期条件:

public static final ExpectedCondition<Boolean> WAIT_FOR_BASKET = ExpectedConditions.visibilityOfElementLocated(By.cssSelector("c-menuimage"));

我有很多他们在一个实用类,所以他们是静态的。你可以做你想要的。

完整的用法看起来像:

WebElement elem = waitForPageToLoad(waitForPageToLoad); 
elem.click();

这个解决方案起源于stack overflow,但我找不到原始的问题/答案,所以我要感谢发布这个的真正的人

 类似资料:
  • 我正在使用for循环来处理表元素。在第一次迭代中,它将在页面上搜索所需的元素。如果该元素在该页面上不可用,那么它将在第二个页面上搜索。如果元素在第一个页面上可用,Webdriver会成功地找到该元素,但如果元素在第一个页面上不可用,则它会在第二个页面上查找该元素。但在这里,for循环失败,出现称为“stale Element exception”的异常。 错误消息: 线程“main”org.ope

  • 我需要访问'Enterprise'和'Enter'文件夹的folderId。 这是我的代码: 字符串S1=driver.findElement(By.xpath(“//SPAN[.='\”Enterprise\“']/祖先::pre/follows-sibling::pre[1]/SPAN[2]”)).getText(); 我想打印字符串s1和s2的值,而不带COTES。我可以访问'Enterpr

  • 下面是我正在努力工作的代码。 我从一个站点地图页面获取所有锚元素,然后将所有这些元素放入一个列表中。然后,我使用从所有这些元素获取URL。在这里之前,代码运行良好。 然而,之后我将这些URL作为参数,并将其传递到方法,以使用。代码一直工作到第一个链接,但一旦加载了第一个页面,就会出现stale元素异常。

  • 问题内容: 我的网站上有一个选择控件。我正在使用页面对象与页面进行交互。如果我这样做(在我的课程下的前两行和我的方法中) 它以空指针失败。我也尝试了没有。 现在,如果我在我的方法中执行此操作,则一切正常,然后选择正确的项目 这是该控件的实际网页摘要(已编辑以保护无辜者) 让我说我可以解决我的问题, 但是 我不明白为什么“ 正常 ”路径无法正常工作。 问题答案: 那是因为该类具有以下构造函数: 见J

  • 我正在使用以下代码检索我的链接: 接下来,我呼吁: 然后 并且抛出过时的元素异常。 现在,考虑到,我本以为可以避免这个问题,但它仍然存在。 我很想在加载页面源代码后,将其放入lxml中,以完全避免这个问题。 建立和迭代链接之间的时间最多为一秒钟。 有没有其他人遇到过这样的问题,并找到了解决方案? 感谢您的指导。