【问题标题】:Unable to locate the child iframe element which is within the parent iframe through Selenium in Java无法通过 Java 中的 Selenium 定位父 iframe 中的子 iframe 元素
【发布时间】:2018-11-06 04:54:46
【问题描述】:

到目前为止,这就是我所拥有的:我已将框架切换到其父框架,然后切换到我无法找到的框架:

By frame = By.xpath("//iframe[@class='GenCss_style-Model']");

driver.switchTo().frame(driver.findElement(By.name("documentflowdesk")));
driver.switchTo().frame(driver.findElement(frame));

由于未找到“框架”元素,我收到此错误:

org.openqa.selenium.NoSuchElementException: 无法定位元素

HTML:

<iframe src="about:blank" name="documentflowdesk" class="gwt-Frame-NavigationComponentViewImplResourcesapplicationFrame" id="documentflowdesk" style="position: absolute; left: 0px; top: 0px; visibility: visible; height: 100%;"></iframe>    
#documentflowdesk
<html style="overflow: hidden;">
    <head>...</head>
    <body style="margin: 0px;" class="dragdrop-dropTarget dragdrop-boundary">
    <noscript>...</noscript>
    <div style="position: absolute; overflow: hidden; left: 0px; top: 0px; right: 0px; bottom: 0px;">
        <div class="css-DeskStyleResources" style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px;">
            <div style="position: absolute; overflow: hidden; left: 0px; top: 0px; right: 0px; bottom: 0px;">
                <div style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px;">
                    <div style="position: absolute; overflow: hidden; left: 0px; top: 0px; right: 0px; bottom: 0px;">
                        <iframe class="GenCss_style-Model" src="/model/?modelId=100&amp;docGroupId=164&amp;connectionPointId=73" style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px;"></iframe>

我试图定位的 iframe 位于多个 div 中。它与错误有什么关系吗?或者我如何找到我的元素有问题吗?

【问题讨论】:

    标签: java selenium selenium-webdriver iframe webdriverwait


    【解决方案1】:

    根据 HTML,一旦您位于 父框架,您必须:

    • 诱导 WebDriverWait 使 父框架 可用并切换到它。
    • 诱导 WebDriverWait 使 子框架 可用并切换到它。
    • 您可以使用以下解决方案:

      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@class='gwt-Frame-NavigationComponentViewImplResourcesapplicationFrame' and @id='documentflowdesk']")));
      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@class='GenCss_style-Model' and contains(@src,'connectionPointId')]")));
      

    在这里你可以找到Ways to deal with #document under iframe的相关讨论

    【讨论】:

      【解决方案2】:

      请尝试以下代码:

      driver.switchTo().frame(driver.findelement(By.xpath(//iframe[@name='documentflowdesk']);
      
      Thread.sleep(5000);
      
      driver.switchTo().frame(driver.findelement(By.xpath(//ifame[@class='GenCss_style-Model']);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-07-31
        • 1970-01-01
        • 1970-01-01
        • 2021-01-20
        • 2022-10-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多