selenium - 将焦点切换到 Chrome 中重新打开的选项卡 - selenium
问题描述
我正在尝试在 selenium 中实现以下流程,
- 单击链接以从父窗口打开新选项卡(child1)
- 执行某些操作后关闭驱动程序(child1)
- 再次打开相同的链接(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
我想创建更像可重用的独立操作的操作,因此我想关闭所有选项卡并再次重新打开它们以进行其他操作。如果您需要更多详细信息,请告诉我。
解决方案
推荐阅读
- java - HTTPServletRequest 对象 - 封装 - 最佳实践
- java - 无法反序列化为具体的 Java 类型
- wordpress - 更新旧插件
- pytest - 如何使用 tox+pytest 启用对 DeprecationWarning 和 PendingDeprecationWarning 的测试
- typescript - 定义某种类型的通用打字稿排序函数
- ruby-on-rails - Rails db:seed 未在 Ubuntu 16.04 上填充 postgresql
- node.js - 无法初始化 MongoDB
- php - 从回调返回值
- python - “ListedColormap”类型的对象没有 len()
- ios - 带有旋转图像的动画