首页 > 解决方案 > 将焦点切换到 Chrome 中重新打开的选项卡 - selenium

问题描述

我正在尝试在 selenium 中实现以下流程,

  1. 单击链接以从父窗口打开新选项卡(child1)
  2. 执行某些操作后关闭驱动程序(child1)
  3. 再次打开相同的链接(child1)以执行另一组操作

我可以通过切换焦点成功完成前两个步骤。但我被困在第三步,我无法专注于同一个重新打开的选项卡。我收到以下错误,org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attach to the page document

public class abc {

String currentWindow = driver.getWindowHandle();

public void action1() {

//Click on main menu that open a new tab 
for (String handle : driver.getWindowHandles()) {
        if (!handle.equals(currentWindow)) {
            driver.switchTo().window(handle);
        }

//Perform the actions 
driver.close();
driver.switchTo().window(currentWindow);

}

public void action2(){

//Click on main menu which reopen the same tab
for (String handle : driver.getWindowHandles()) {
                if (!handle.equalsIgnoreCase(currentWindow)) {
                    System.out.println("Port Response : switch focus");
                    driver.switchTo().window(handle);
                    break;
                }
            }
//perform a set of actions 
driver.close();
driver.switchTo().window(currentWindow);
}

}//end of abc

我想创建更像可重用的独立操作的操作,因此我想关闭所有选项卡并再次重新打开它们以进行其他操作。如果您需要更多详细信息,请告诉我。

标签: selenium

解决方案


推荐阅读