如何使用带有 Java 的 selenium webdriver 处理弹出窗口 [英] How to handle popup window using selenium webdriver with Java

查看:40
本文介绍了如何使用带有 Java 的 selenium webdriver 处理弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮忙,我是 Selenium 的新手.我尝试自动化电子商务网站,但处理弹出窗口时遇到问题.这是场景:

Please help, I'm new in Selenium. I try to automate eCommerce website and I have problem to handle popup window. Here is the scenario:

  1. 转到 http://www.lampsplus.com
  2. 点击标题部分的销售链接.
  3. 单击页面上显示的第一个项目/产品.(这将显示产品页面).
  4. 在产品页面上,点击红色的加入购物车"按钮.(这会将产品添加到购物车并显示一个弹出窗口).
  5. 在弹出窗口中,点击深灰色的继续购物"按钮.(这将关闭弹出窗口.)

我停留在第 5 步(错误消息:无法找到元素继续购物按钮")这是我在第 5 步之前的代码:

I stuck on step 5 (Error message: Unable to locate element "Continue shopping button") Here is my code before step 5:

 WebDriver d1 = new FirefoxDriver();
d1.manage().window().maximize();
d1.get("http://www.lampsplus.com");
d1.findElement(By.name("hdr_sale")).click();
d1.findElement(By.xpath(".//*[@id='sortResultContainer60238']/a[2]/span")).click();
d1.findElement(By.id("pdAddToCart")).click(); //This is step 4
//Here is suppose to be some code which handles the popup - my problem
d1.findElement(By.id("aContinueShopping")).click();  //This is step 5

我知道 .getWindowHandle();方法.我尝试了它的几种变体,但都没有奏效.谁能给我一个想法如何处理它.非常感谢!我使用 Java.

I'm aware about .getWindowHandle(); method. I tried several variations of it and none of them worked. Can anyone give me an idea how to handle it. Many thanks! I use Java.

注意:我不为 LampsPlus 工作,也不会尝试推广他们的产品,选择此网站仅用于培训目的.

Note: I don't work for LampsPlus and not try to promote their products, this website was chosen for training purposes only.

推荐答案

元素 aContinueShopping 包含在 iframe 中.

The element aContinueShopping is contained within an iframe.

您必须使用以下命令切换到 iframe:

You'll have to switch to the iframe using:

WebElement frameID = d1.findElement(By.Css("#add-to-cart>iframe"));
d1.SwitchTo().Frame(frameID);
d1.findElement(By.id("aContinueShopping")).click();

iframe 上没有名称"或ID",因此您必须使用 WebElement 或数字才能找到它.

There's no 'name' or 'id' on the iframe, so you'll have to use a WebElement or a numeric to find it.

完成该 iframe 后,您将使用以下命令切换回顶部":

Once you're done with that iframe, you'll switch back to 'top' by using:

d1.SwitchTo().DefaultContent();

这篇关于如何使用带有 Java 的 selenium webdriver 处理弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆