【问题标题】:Select a specific iFrame that has multiple instances of the same name选择具有多个同名实例的特定 iFrame
【发布时间】:2016-05-26 09:44:17
【问题描述】:

我的问题是我正在尝试处理的页面上有 3 个已定义的 iFrame。它们都被命名为:

<iframe class="col-51" height="560" src="https://attendee.gototraining.com/embed/886b2/catalog/40xxxx bgColor=ffffff" frameborder="0"></iframe>

<iframe class="col-51" height="560" src="https://attendee.gototraining.com/embed/886b2/catalog/37xxxx bgColor=ffffff" frameborder="0"></iframe>

<iframe class="col-51" height="560" src="https://attendee.gototraining.com/embed/886b2/catalog/46xxxx bgColor=ffffff" frameborder="0"></iframe>

我尝试了 driver.switchTo.frame 命令,但它只看到索引 0,我认为它是此处列出的第一帧。如果我选择这第一帧,我不会在运行时收到错误,但是我仍然看不到帧内的任何元素。我得到“找不到元素”的问题。我什至无法使用我制作的定位或索引 1 或 2 选择带有 switchTo 的第 2 帧或第 3 帧。

我也尝试过制作一个包含 iframe 的所有标记名的列表。我只得到 1 个结果。同样,我在这里看到 3。同样,即使我选择了第一个,我仍然无法使用常规方式看到框架内的任何元素。

我在这里错过了什么?

另外请注意,出于隐私考虑,我在路径中添加了 x。

如果我选择 0 以外的其他索引会发生什么情况:

org.openqa.selenium.NoSuchFrameException: Unable to locate frame: 1
Command duration or timeout: 62 milliseconds
Build info: version: '2.52.0', revision: '4c2593c', time: '2016-02-11 19:03:33'
System info: host: 'DESKTOP-1PV0EPA', ip: '192.168.0.11', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_77'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=46.0.1, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]

【问题讨论】:

    标签: java selenium iframe


    【解决方案1】:

    您可以使用 xpath 定位器并通过部分src 链接识别帧:

    void switchToIframe(int catalogId)
    {
        driver.switchTo().defaultContent(); // don't get trapped in iframes
        driver.switchTo().frame(
            driver.findElement(
                By.xpath("//iframe[contains(@src, 'catalog/" + catalogId + "')]"));
    }
    

    像这样使用它:

    switchToIframe(40); // frame 1
    switchToIframe(36); // frame 2
    switchToIframe(46); // frame 3
    

    在切换到它们之前,您可能还需要等待帧加载

    【讨论】:

    • 好的,我试了一下,没有遇到这样的元素异常。
    【解决方案2】:

    我建议尝试通过它们的 XPath 将 iframe 解析为 WebElement。下面的代码 sn-p 存根测试用例,您需要确定每个 iframe 的 xpath 并填写字符串,但我认为这应该很接近。

        private static final By FRAME_PATH_1 = By.xpath("");
        private static final By FRAME_PATH_2 = By.xpath("");
        private static final By FRAME_PATH_3 = By.xpath("");
    
        private void traverseFrames(WebDriver driver) {
            WebDriverWait wait = new WebDriverWait(driver, 10);
    
            WebElement frame1 = wait.until(ExpectedConditions.presenceOfElementLocated(FRAME_PATH_1));
            WebElement frame2 = wait.until(ExpectedConditions.presenceOfElementLocated(FRAME_PATH_2));
            WebElement frame3 = wait.until(ExpectedConditions.presenceOfElementLocated(FRAME_PATH_3));
    
            wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frame1));
            System.out.println("in Frame 1");
    
            wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frame2));
            System.out.println("in Frame 2");
    
            wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frame3));
            System.out.println("in Frame 3");
    
        }
    

    如果您希望页面内容发生变化,XPath 的可维护性并不是很好,但它可能会让您更接近您正在寻找的内容。我使用 Firebug 的 Firepath 扩展来帮助解决 xpath。

    祝你好运

    【讨论】:

      猜你喜欢
      • 2021-08-08
      • 1970-01-01
      • 1970-01-01
      • 2019-02-10
      • 2017-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多