如何在 selenium webdriver 中从一个弹出窗口切换到另一个弹出窗口

2024-01-20

我的场景是:

  1. 主窗口-> 进行一些活动。
  2. 单击“保存”按钮 -> 弹出确认窗口,并使用“确定”和“取消”按钮打开。
  3. 单击确认弹出窗口上的“确定”按钮 -> 使用“确定”按钮打开另一个成功弹出窗口。
  4. 弹出成功后点击确定按钮。
  5. 切换到主窗口。

PopUp 上方是 HTML 弹出窗口。 我如何在硒中处理上述场景?我是硒的新手。请帮助我。我被困在上面的观点上。

Code

String ParentWindow = driver.getWindowHandle(); //switching from parent to pop up window 
for (String Child_Window : driver.getWindowHandles()) { 
driver.switchTo().window(Child_Window);
WebDriverWait wait = new WebDriverWait(driver, 30);// 1 minute 
wait.until(ExpectedConditions.visibilityOfElementLocated(By.‌​name("test")));   
driver.findElement(By.xpath("//input[@value='test']")).click‌​();
} 
driver.switchTo().window(ParentWindow); 

HTML

<div>
<div class="msgBoxContainer">
<div id="msgBox1473308035532Image" class="msgBoxImage">
<img src="styles/images/confirm.png">
</div>
<div id="msgBox1473308035532Content" class="msgBoxContent">
<p>
<span>Saveでよろしいですか??</span>
</p>
</div>
</div>
<div id="msgBox1473308035532Buttons" class="msgBoxButtons">
<input id="msgBox1473308035532FirstButton" class="msgButton" type="button"  value="はい" name="はい">
<input class="msgButton" type="button" value="いいえ" name="いいえ">
</div>
</div>
</div>

// 当单击第一个弹出窗口的“确定”按钮时,相应的 div 被销毁,并为第二个弹出窗口生成新的 div

<div id="msgBox1473308225709" class="msgBox" style="background-image:     url("styles/images/msgBoxBackGround.png"); opacity: 1; top: 52.5px; left: 566.5px;">
<div class="msgBoxTitle">Information</div>
<div>
<div class="msgBoxContainer">
<div id="msgBox1473308225709Image" class="msgBoxImage">
<img src="styles/images/info.png">
</div>
<div id="msgBox1473308225709Content" class="msgBoxContent">
<p>
<span>登録完了</span>
</p>
</div>
</div>
<div id="msgBox1473308225709Buttons" class="msgBoxButtons">
<input id="msgBox1473308225709FirstButton" class="msgButton" type="button"   value="はい" name="はい">
</div>
</div>
</div>

单击“保存”按钮后,您应该按如下方式处理这些信息对话框:-

WebDriverWait wait = new WebDriverWait(driver,10);

//For first dialog box
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input.msgButton[id*='msgBox'][id*='FirstButton']"))).click();

//Now same as for second dialog box
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input.msgButton[id*='msgBox'][id*='FirstButton']"))).click();

Note:- 不需要切换窗口,这些对话框都是简单的HTML元素,所以你需要找到这些元素来正常处理。

Edited1:- 如果您无法点击使用WebElement.click()尝试使用Actions在单击之前移动该元素的类,如下所示:-

Actions act = new Actions(driver);

//For first dialog box
WebElement firstDialog = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input.msgButton[id*='msgBox'][id*='FirstButton']")));
act.moveToElement(firstDialog).click().perform();

//Now same as for second dialog box
WebElement secondDialog = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input.msgButton[id*='msgBox'][id*='FirstButton']")));
act.moveToElement(secondDialog).click().perform();

Edited2:- 如果您仍然无法点击,请尝试使用JavascriptExecutor如下 :-

//For first dialog box
WebElement firstDialog = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input.msgButton[id*='msgBox'][id*='FirstButton']")));
((JavascriptExecutor)driver).executeScript("arguments[0].click()",firstDialog);

//Now same as for second dialog box
WebElement secondDialog = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input.msgButton[id*='msgBox'][id*='FirstButton']")));
((JavascriptExecutor)driver).executeScript("arguments[0].click()",secondDialog);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 selenium webdriver 中从一个弹出窗口切换到另一个弹出窗口 的相关文章

  • 代理阻止网络套接字?如何绕行

    我有一个用 Python 编写的正在运行的 websocket 服务器 来自https github com opiate SimpleWebSocketServer https github com opiate SimpleWebSoc
  • 是否有适用于 Java 的 CalDAV 客户端库? [关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 我想使用 CalDAV 协议与我的日
  • 在 forEach() 中使用 `this` [重复]

    这个问题在这里已经有答案了 免责声明 我正在学习 JavaScript 我有一个像这样的对象构造函数 var Palette function this colors red green blue this getColorCombinat
  • 在休眠搜索中使用现有分析器AnalyzerDiscriminator

    Entity Indexed AnalyzerDefs AnalyzerDef name en tokenizer TokenizerDef factory StandardTokenizerFactory class filters To
  • 使用 AngularJS $resource 进行 jsonp 请求

    我在 AngularJS 中定义了以下 2 个服务 两者都应该返回 JSONP 因为我正在进行跨域请求 服务A angular module ServiceA ngResource factory A function resource r
  • 更改JavaFX TableView字体大小[重复]

    这个问题在这里已经有答案了 您好 我想在表视图列内的文本上设置字体 我如何用 Java 做到这一点 这是我的代码 感谢帮助 private final TableView
  • Selenium 网页抓取与动态内容和隐藏数据表上的美丽汤

    真的需要这个社区的帮助 我正在使用 Selenium 和 Beautiful Soup 对 Python 中的动态内容进行网页抓取 问题是定价数据表无法解析为 Python 即使使用以下代码 html browser execute scr
  • PHP:在执行 php 脚本时显示“正在加载”页面

    这就是我现在所拥有的 我有一个网页 当访问该网页时 它会通过开放 API 连接到 Surveygizmo com 检索大量数据 然后将这些数据返回给我进行处理 此过程大约需要 10 12 秒 在执行时 页面只是处于 正在加载 状态 并且我会
  • mysql 准备好的语句错误:MySQLSyntaxErrorException

    我使用准备好的语句编写了选择语句 每次尝试运行都会出现此错误 我如何克服这个错误 我的jdbc连接器是mysql connector java 5 1 13 bin jar 我的代码 public Main add ad to getAdD
  • 使用Doctype让scrollTop返回0,为什么?

    当我将此 Doctype 放入我的文档中时document body scrollTop返回零 这是为什么 当您使用该 Doctype 时 您会将每个当前浏览器放入所谓的几乎标准模式 http hsivonen iki fi doctype
  • 如何在 Spyder IDE 中安装 Selenium 包

    我刚刚在工作中安装了 Spyder IDE 仅 Spyder 不是整个 Anaconda 并且希望使用 FireFox 自动化我的工作 我的问题是 如何安装 Selenium 软件包 I figured it out Here is ins
  • 输入号码时自动格式化 SSN

    我有一个文本字段 用户输入 SSN 号码 输入自身时 它应该格式化 就像关于文本字段的更改 它应该格式化999 999 999以这种方式在显示器本身上 kottenator 的脚本几乎已经完成 但它每隔 3 位数字就中断该值 而不是 3 位
  • bootstrap-datetimepicker 仅显示日期

    我正在用这个repo https github com smalot bootstrap datetimepicker由 smalot 提供 我只想选择并显示日期 对于其他一些地方 我显示数据和时间 因此选择此存储库 我可以设法仅使用它来选
  • 为什么找不到 getservletcontext?

    我正在尝试使用getServletContext getRealPath 但我不断收到此错误 cannot find symbol symbol method getServletContext location interface jav
  • 如何实现 chromecast 对 html5 播放器的支持

    我使用js和html5设计了一个具有一些自定义功能的html5播放器 现在我需要在html5播放器上添加chromecast选项 例如https raw githubusercontent com kim company videojs c
  • 找出对象列表中是否包含具有指定字段值的内容?

    我有一个从数据库收到的 DTO 列表 它们有一个 ID 我想确保我的列表包含具有指定 ID 的对象 显然 在这种情况下创建具有预期字段的对象不会有帮助 因为 contains 调用 Object equals 并且它们不会相等 我想出了这样
  • 当框架被拖动时,如何设置 JWindow 的位置位于文本字段下方?

    我正在制作一个自动完成项目 就像谷歌一样 我的框架中有一个 jtextfield 每当我在该字段中输入内容时 该文本字段下方就会出现一个 JWindow 并且该窗口来自另一个类 现在的问题是 每当我拖动框架时 如何使窗口始终出现在文本字段下
  • Twitter 引导选项卡和 JavaScript 事件

    我正在一个项目中使用 twitter bootstrap 特别是它的选项卡功能 http twitter github com bootstrap javascript html tabs http twitter github com b
  • 显示对象内容 - JS/jQuery

    With this data events 返回 object Object 我需要看看里面到底发生了什么 我找到了这个 var Finder each this data events function i n Finder Name i
  • Android NDK - 仅用 C/C++ 编写

    有没有一种可能的方法可以使用 C C 编写整个 NDK 应用程序 而无需像 hello jni 示例项目 HelloJni java 中那样的 Java 入门 类 以某种方式创建一个 HelloJni c 来执行相同的操作 从 Androi

随机推荐