如何在selenium中单击iframe弹出窗口中的关闭(X)图标

时间:2016-02-09 09:23:22

标签: selenium

//请检查以下代码中的关闭按钮

<div class="fancybox-wrap fancybox-desktop fancybox-type-iframe fancybox-opened" tabindex="-1" style="width: 557px; height: auto; position: absolute; top: 20px; left: 396px; opacity: 1; overflow: visible; display: block;">
    <div class="fancybox-skin" style="padding: 0px; width: auto; height: auto;">
    <div class="fancybox-outer">
    <div class="fancybox-inner" style="overflow: auto; width: 557px; height: 385px;">
    <iframe id="fancybox-frame1455168443258" class="fancybox-iframe" frameborder="0" allowfullscreen="" mozallowfullscreen="" webkitallowfullscreen="" hspace="0" vspace="0" name="fancybox-frame1455168443258" scrolling="auto" src="http://100stohappiness.dev-imaginovation.net/100s-happiness/100s-happiness/login" style="height: 460px;"/>
    </div>
    </div>
    <a class="fancybox-item fancybox-close orange-color-bg" href="javascript:;" title="Close"/>
    </div>
    </div>

1 个答案:

答案 0 :(得分:0)

首先需要切换到iframe

  

Java语法,类似于所有语言

String parentHandle = driver.getWindowHandle();

// switch to the new window
for (String handle : driver.getWindowHandles()) {
    if (!handle.equals(parentHandle))
    {
        driver.switchTo().window(handle);
    }
}

// click on close button
driver.findElement(By.className("fancybox-close")).click();

// switch back to the old window
driver.switchTo().window(parentHandle);