【问题标题】:Selenium and iframe硒和 iframe
【发布时间】:2011-11-26 09:52:06
【问题描述】:

我有一个 iframe,当我单击页面上的选项卡时会加载该 iframe。当我使用 Firebug 查看 IE8 上的 iframe 时,我看到的只是:

iframe id=tabContextFrame class=contextFrame contentEditable=inherit src=/xyz.dt?forward=show&layouttype=NoHeader&runid=1234 name=tabContextFrame url=/xyz.dt?forward=show&layouttype=NoHeader&runid=1234 scrolling=auto

就是这样。无法看到 iframe 下方的层次结构。我想点击 iframe 中的链接。为了在 iframe 中查找元素,我执行了 selenium.click("on the tab that loads the iframe")selenium.getHtmlSource()。从这个来源,我至少可以找到我感兴趣的链接。我做了一个selenium.click("//span[text()='Link']"),但它似乎没有做任何事情。请问有什么想法吗?

代码如下:

selenium.click("//span[text()='tab that loads iframe']");   
Thread.sleep(5000);   
selenium.selectFrame("tabContextFrame");         
selenium.mouseOver("//span[text()='Link']");   
selenium.mouseDown("//span[text()='Link']");  
selenium.mouseUp("//span[text()='Link']");   
Thread.sleep(5000);   
selenium.selectFrame("null");  

【问题讨论】:

  • 您是否尝试过使用 web 驱动程序的 selenium 2.0?你可以使用 driver.SwitchTo().Frame()

标签: iframe selenium


【解决方案1】:

使用 driver.switchTo().defaultContent();先做手术

【讨论】:

    【解决方案2】:

    我猜你正在使用 Selenium 1.0。你看过 Selenium 2.0 和 WebDriver。我发现了以下内容,它对我有用:

    问:如何在 contentEditable iframe 中输入内容?答:假设 iframe 被命名为“foo”:

    driver.switchTo().frame("foo");
    WebElement editable = driver.switchTo().activeElement(); 
    editable.sendKeys("Your text here"); 
    

    有时这不起作用,这是因为 iframe 没有任何内容。在 Firefox 上,您可以执行以下操作 在“sendKeys”之前:

    ((JavascriptExecutor) driver).executeScript("document.body.innerHTML = '<br>'"); 
    

    这是必需的,因为 iframe 默认没有内容: 没有什么可以发送键盘输入。此方法调用插入一个 空标签,可以很好地设置所有内容。

    请记住在完成后切换出框架(正如所有进一步的 交互将与这个特定的框架):

    driver.switchTo().defaultContent();
    

    我在http://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions找到了这个

    【讨论】:

      猜你喜欢
      • 2021-04-07
      • 1970-01-01
      • 1970-01-01
      • 2022-01-12
      • 2021-08-09
      • 2015-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多