【问题标题】:Exception in thread “main” org.openqa.selenium.StaleElementReferenceException: Element not found in the cache线程“主”org.openqa.selenium.StaleElementReferenceException 中的异常:在缓存中找不到元素
【发布时间】:2015-07-20 12:24:17
【问题描述】:

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

“线程“主”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();
  }
}

【问题讨论】:

  • 你在哪一行得到了异常?
  • 在hovering.perform();@peetya
  • 如果你在没有尝试的情况下启动代码并捕获它,在这种情况下什么都不会发生

标签: java multithreading selenium webdriver


【解决方案1】:

不要在同一上下文中混合 ImplicitWait ExplicitWait 和 Thread.Sleep。
Learn 何时何地应该是 used。

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();
}
}

【讨论】:

  • 成功了,非常感谢。感谢您提供有关超时的提示
【解决方案2】:

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

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

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

【讨论】:

  • 你的意思是这样的——对吧? while(!submenu.isEmpty()){ String b = null; for (int i=0;i&lt;=submenu.size()-1;i++ ) { b = submenu.get(i); } hovering.perform(); Thread.sleep(1000);clickAnElementByLinkText(b);Thread.sleep(1000); driver.navigate().back(); }@Avishay
  • 尝试更改和移动行:IReadOnlyCollection a = driver.findElements(By.cssSelector("#nav > div.navMain > div.nav_menu > nav > ul > li:nth-child (1) > a")).ElementAt(i);和动作悬停 = action.moveToElement(a).build();在行下: for (int i=0;i
  • IReadOnlyCollection 应该只是 WebElement
猜你喜欢
  • 1970-01-01
  • 2018-02-22
  • 1970-01-01
  • 2015-12-08
  • 1970-01-01
  • 2021-03-10
  • 2019-05-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多