【问题标题】:Unable to locate and switch to iframe using Eclipse with Selenium webdriver Java无法使用带有 Selenium webdriver Java 的 Eclipse 定位并切换到 iframe
【发布时间】:2016-01-15 04:34:44
【问题描述】:

当我点击一个链接时,会打开一个弹出窗口,我可以在其中填写一些字段。但是,此弹出窗口包含一个 Iframe,其中包含我要在测试中使用的字段。问题是我似乎无法切换到 iFrame 以访问字段。

我首先尝试了以下方法,但这会导致 (org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"/html/body/iframe" }):

driver.switchTo().frame(driver.findElement(By.xpath("/html/body/iframe")));

然后我尝试了以下方法,这似乎通过了 iFrame 但导致 (org.openqa.selenium.TimeoutException: Timed out after 15 seconds waiting for element to be clickable: By.id: //div[@class= 'actionbutton-content unselectable']) 和 (NoSuchElementException: Unable to locate element: {"method":"id","selector":"//div[@class='actionbutton-content unselectable']"}):

driver.switchTo().frame(0);
wait.until(ExpectedConditions.elementToBeClickable(By.id("//div[@class='actionbutton-content unselectable']")));

这是页面源代码的第一部分:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <title>Page title</title>
    <script src="scripts/JQuery/jquery-1.4.2.min.js" type="text/javascript"></script>
    <script type="text/javascript" language="javascript">
    script here....
    </script>
</head>

<body MS_POSITIONING="GridLayout" leftmargin="0" rightmargin="0" topmargin="0" bottommargin="0">
    <input type="hidden" id="newxObjectID"> <input type="hidden" id="newxObjectName"/>
    <iframe height="100%" width="100%" frameborder="0" scrolling="no" src="fxeditobject.aspx?function=showobjectfields&key=0x3928x0x60937x&viewid=&callerwindow=true&fieldDefId=0&defid=3928"  >
    #document
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html webdriver="true">
    <head></head>
    <body id="PageBody" onload="" etc. etc. etc. >
**Fields I'm looking for are somewhere in this area**
    </iframe>
</body>

【问题讨论】:

  • 尝试向 iframe 添加 id
  • 谢谢,但这不是我的产品,所以很遗憾不是一个选择:-(
  • 弹出窗口是否在单独的浏览器窗口中打开?或者您得到的弹出窗口是一种 jquery 类型的模态对话框?
  • 是的,弹出窗口正在打开,看起来像是一个单独的浏览器窗口。

标签: java eclipse selenium iframe


【解决方案1】:

您尝试过稍微不同的 xpath 吗? -> 双斜线"//html/body/iframe"

driver.switchTo().frame(driver.findElement(By.xpath("//html/body/iframe")));

如果这不起作用,可能 iframe 没有直接位于您的 body 标签下(您发布的是完整的 html 还是 sn-p?),那么您在 iframe 前面再添加一个双斜杠:

driver.switchTo().frame(driver.findElement(By.xpath("//html/body//iframe")));

至于你的第二个问题:

如果您提供 xpath 表达式,您还应该搜索 By.xpath(您搜索的是 By.id):

driver.switchTo().frame(0);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='actionbutton-content unselectable']")));

【讨论】:

    【解决方案2】:

    不确定这是否是可行的方法,但我似乎找到了解决方案,或者至少找到了解决方法。在单击打开新窗口的链接的代码行之后,我将代码放在下面:

    Set <String> handles =driver.getWindowHandles();
    Iterator<String> it = handles.iterator();
    //iterate through your windows
    while (it.hasNext()){
    String parent = it.next();
    String newwin = it.next();
    driver.switchTo().window(newwin);
    
    driver.switchTo().defaultContent();
    driver.switchTo().frame(driver.findElement(By.xpath("//html/body/iframe")));
    

    据我了解代码(我已经使用 google 组装它:-))代码的第一部分是切换到刚刚打开的窗口。然后我切换到默认内容以确保我不会不情愿地处于任何类型的任何框架中。然后我切换到包含我需要填写或测试的字段的 iFrame。 现在尽管这项工作,它似乎需要 10 分钟,我不知道为什么会这样?有没有办法弄清楚哪些步骤需要这么长时间以及为什么?

    【讨论】:

      猜你喜欢
      • 2017-05-06
      • 1970-01-01
      • 2023-03-08
      • 2017-12-24
      • 2018-12-31
      • 2021-06-19
      • 1970-01-01
      • 1970-01-01
      • 2022-10-22
      相关资源
      最近更新 更多