Selenium无法在带有iframe的模态窗口中找到元素 [英] Selenium is unable to find element in a Modal window with iframes

查看:232
本文介绍了Selenium无法在带有iframe的模态窗口中找到元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,我点击一个链接,打开一个新的模态窗口,其中包含一个iframe。我切换到iframe并执行了一些验证,然后单击该Modal窗口中的链接,该窗口依次打开带有iframe的第二个新Modal窗口。我在第二个新模态窗口中点击任何元素时遇到问题。

I have a page in which I click on a link which opens a new Modal window which has an iframe. I switched to the iframe and performed some validation, then click on the link in that Modal window which in turn opens a second new Modal window with an iframe. I am facing issue clicking on any element in that second new Modal window.

这是我的代码。

WebElement Hotelname = driver.findElement(By.cssSelector(".hotelTitleZone2>a"));
Hotelname.click(); \\This will open a new Pop up.

driver.switchTo().frame(1);
\\perform some validation
String parentHandle = driver.getWindowHandle();
driver.findElement(By.linkText("View on a Map")).click(); \\This will open second pop up Modal window

for (String winHandle : driver.getWindowHandles()) {
    driver.switchTo().window(winHandle); 
}
driver.switchTo().defaultContent();
driver.switchTo().frame(1); \\switching to frame
driver.findElement(By.linkText("Close")).click();

当我运行此代码时,我收到错误:

When I am running this code, I am getting an error:


org.openqa.selenium.NoSuchElementException:无法找到元素:
{method:link text,selector :关闭}

org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"link text","selector":"Close"}

我尝试使用或不使用切换到默认内容,而不在第二个模态窗口中切换到帧,但结果是总是一样。

I tried with or without switching to default content, without switching to frame in second Modal window but result is always the same.

任何帮助表示赞赏?谢谢。

Any help is appreciated ? Thanks.

推荐答案

我的理解是:


  1. 从默认窗口开始

  2. 点击打开第一个包含iframe的模态窗口

  3. 切换到这个新的iframe(index = 1 )

  4. 获取当前窗口句柄的ID,这是默认窗口句柄

  5. 单击打开第二个具有第二个iframe的Modal窗口

  6. 切换到第二个模态窗口

  7. 切换回默认窗口

  8. 切换到iframe(索引) = 1)

  9. 找到你想要的按钮

  1. start from the default window
  2. click to open the first Modal window that has an iframe
  3. Switch to this new iframe (index = 1)
  4. Get the ID for the current window handle, which is the default window handle
  5. click to open the second Modal window that has a second iframe
  6. switch to the second Modal window
  7. switch back to the default window
  8. switch to iframe (index = 1)
  9. Find the button you are after

这里有一些混淆:


  • 在上面的步骤4中,您使用了String parentHandle = driver.getWindowHandle();存储原始的默认窗口句柄,但你从未使用它,而是使用driver.switchTo()。defaultContent();

  • 单击后第一个Modal窗口发生了什么?它关闭了吗?如果它还没有关闭,它的iframe仍然是 iframe(index = 1),这可以解释为什么你无法从 iframe(index = 1)找到你的按钮;因为您的按钮将驻留在属于第二个模态窗口的iframe上,该窗口很可能是 iframe(index = 2)。您可以使用 driver.switchTo()。frame(2); 来代替它。当然,您可以检查HTML元素以查看有多少iframe存在以及它们属于哪个模态窗口。

  • in step 4 above, you used String parentHandle = driver.getWindowHandle(); to store the original default window handle but you have never used it, instead, you use driver.switchTo().defaultContent();
  • What happened to the first Modal window after you clicked it? Did it close? if it had not closed, its iframe would still be iframe (index=1), this would explain why you could not find your button from the iframe (index=1); as your button would reside on the iframe that belongs to the second Modal window, which is likely to be iframe (index=2). You may use driver.switchTo().frame(2); to address it instead. To be sure, you can inspect HTML elements to see how many iframes are present and to which Modal windows they belong to.

希望你会发现它很有用。

Hope you will find it useful.

这篇关于Selenium无法在带有iframe的模态窗口中找到元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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