线程“main”中的异常org.openqa.selenium.StaleElementReferenceException:在缓存中找不到元素

时间:2015-07-20 12:24:18

标签: java multithreading selenium webdriver

有人可以帮忙吗? 我有错误

  

“线程中的异常”main“org.openqa.selenium.StaleElementReferenceException:在缓存中找不到元素”

为何显示此错误? 我需要将鼠标悬停在每个类别菜单上,而不是单击子菜单中的每个文本。

public class santander {
  private static WebDriver driver = null;     
  public static JavascriptExecutor js = (JavascriptExecutor) driver;
  public static void main(String[] args) throws FileNotFoundException, InterruptedException, IOException {
        // TODO Auto-generated method stub
        driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("http://www.santander.co.uk/uk/index");




    driver.manage().window().maximize();
    driver.get("http://www.santander.co.uk/uk/index");

    JavascriptExecutor js = (JavascriptExecutor) driver;

    WebDriverWait wait = new WebDriverWait(driver, 10);  

    String submenutxtlinks = "submenu.txt";

    List<String> submenu = new ArrayList<String>();
    BufferedReader reader = new BufferedReader(new FileReader(submenutxtlinks));
    String line;
      while ((line = reader.readLine()) != null) {
          submenu.add(line);
      }
      reader.close();



      Actions action = new Actions(driver);
     /* 
     WebElement menu = driver.findElement(By.linkText("Current Accounts"));
     action.moveToElement(menu).perform();
     WebElement submenu = driver.findElement(By.linkText("See all current accounts"));
     action.moveToElement(submenu);
     action.click();
     action.perform();
     */   



       // String title = driver.getTitle();

       // wait.until(ExpectedConditions.titleIs(title));
      //  driver.navigate().back();

    //Loop to read all lines one by one from file and print It.
     // while((menu = BR.readLine())!= null && !menu.isEmpty()){



         // driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        //  Action hovering = action.moveToElement(a).build();
        //  hovering.perform();

          //action.moveToElement(a).perform();
          //action.clickAndHold(a).perform();
      WebElement a = driver.findElement(By.cssSelector("#nav > div.navMain > div.nav_menu > nav > ul > li:nth-child(1) > a"));
      Action hovering = action.moveToElement(a).build();
      //Thread.sleep(2000);

    for (int i=0;i<=submenu.size()-1;i++ ) {

        //String b = submenu.get(i);

       // System.out.println(b); 

        //WebElement b = driver.findElement(By.xpath(submenu.get(i)));
        try{
            //Your code which causes exception




        hovering.perform();


        //action.moveToElement(b).click(b).build().perform();
        Thread.sleep(1000); 

        clickAnElementByLinkText(submenu.get(i));
        //b.click();
        Thread.sleep(1000);
          /*
        wait.until(ExpectedConditions.visibilityOf(b));
        action.moveToElement(b);
        action.click();
        action.perform();
        */
  //  wait.until(ExpectedConditions.titleIs(title));

      driver.navigate().back();

        //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        Thread.sleep(2000);
   // driver.get("http://www.santander.co.uk/uk/index");
        //driver.navigate();
        Thread.sleep(2000);
  //  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        }
        catch(org.openqa.selenium.StaleElementReferenceException e){
            //Repeat the code in try
            }


}

 // } 

    driver.close();
}



public static void clickAnElementByLinkText(String linkText) {

  WebDriverWait wait = new WebDriverWait(driver, 10);
  wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(linkText)));
  driver.findElement(By.xpath(linkText)).click();
  }
}

2 个答案:

答案 0 :(得分:0)

您正在尝试访问不存在的元素。

  • 代码收集页面上的元素
  • 然后代码点击一个更改源
  • 的元素
  • 然后它尝试访问之前收集的对象,但它不再存在(你看到的是新的)。

您可以在循环中输入元素过程的发现并在每次迭代中循环更改“i”元素(如果必须在每次迭代中单击元素)。

答案 1 :(得分:0)

不要将ImplicitWait ExplicitWait和Thread.Sleep混合在同一个上下文中 Learn used的时间和地点{/ 3}}。

hoverElement不会工作,因为驱动程序导航到不同的页面,元素不再存在,你在forLoop中再次找到它

为了测试我已经硬编码了子菜单

此代码将执行您要求的

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class ListTest {

static WebDriver driver;

public static void main(String[] args) {
    List<String> submenu = new ArrayList<>(Arrays.asList(new String[]{"See all current accounts", "1|2|3 Current Account", "Everyday Current Account", "Basic Current Account", "Choice Current Account"}));
    driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.manage().window().maximize();
    driver.get("http://www.santander.co.uk/uk/index");
    for (String sMenu : submenu) {
        WebElement a = driver.findElement(By.cssSelector("#nav > div.navMain > div.nav_menu > nav > ul > li:nth-child(1) > a"));
        new Actions(driver).moveToElement(a).build().perform();
        clickAnElementByLinkText("//li[@role='listitem']/a[normalize-space(text())='" + sMenu + "']");
        driver.navigate().back();
    }
    driver.quit();
}

public static void clickAnElementByLinkText(String linkText) {
    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(linkText))).click();
}
}