无法通过Java中的Selenium定位位于父iframe中的子iframe元素

时间:2018-11-06 04:54:47

标签: java selenium selenium-webdriver iframe webdriverwait

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

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

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

由于未找到'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中。它与错误有关系吗?还是我的元素查找方式有问题?

2 个答案:

答案 0 :(得分:1)

请尝试以下代码:

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']);

答案 1 :(得分: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的相关讨论