线程“main”中的异常 org.openqa.selenium.StaleElementReferenceException:在缓存中找不到元素
Exception in thread “main” org.openqa.selenium.StaleElementReferenceException: Element not found in the cache
有人可以帮忙吗?
我有错误
"Exception in thread “main” org.openqa.selenium.StaleElementReferenceException: Element not found in the cache"
为什么显示这个错误?
我需要将鼠标悬停在每个类别菜单上,而不是单击子菜单中的每个文本。
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();
}
}
您正在尝试访问一个不存在的元素。
- 代码收集了页面上的元素,
- 然后代码点击了一个改变源的元素
- 然后它尝试访问他在之前收集的一个对象,但它不再存在(你看到的是一个新的) .
可以将查找元素的过程进入循环,每次迭代循环改变'i'元素(如果每次迭代都必须点击元素)。
不要在同一上下文中混用 ImplicitWait ExplicitWait 和 Thread.Sleep。
Learn when and where those should be 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();
}
}
有人可以帮忙吗? 我有错误
"Exception in thread “main” org.openqa.selenium.StaleElementReferenceException: Element not found in the cache"
为什么显示这个错误? 我需要将鼠标悬停在每个类别菜单上,而不是单击子菜单中的每个文本。
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();
}
}
您正在尝试访问一个不存在的元素。
- 代码收集了页面上的元素,
- 然后代码点击了一个改变源的元素
- 然后它尝试访问他在之前收集的一个对象,但它不再存在(你看到的是一个新的) .
可以将查找元素的过程进入循环,每次迭代循环改变'i'元素(如果每次迭代都必须点击元素)。
不要在同一上下文中混用 ImplicitWait ExplicitWait 和 Thread.Sleep。
Learn when and where those should be 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();
}
}