无法在Selenium Webdriver中选择框架

时间:2015-02-12 00:35:12

标签: java selenium

我知道这个问题已被问过几次,但我一直无法弄清楚我的问题。我正在尝试选择'fraHeader'框架,但我能得到的是'无法找到元素'错误。我正在使用带Java的Webdriver

我尝试过的事情:

  1. 使用driver.findElement查找帧,然后使用driver.switchTo切换到它 - 这不起作用。我一直得到'无法找到元素'。

  2. 我尝试使用xpath,id和name来定位框架,但都没有工作。

  3. 使用Selenium IDE记录操作并导出到Java - 这给了我://错误:捕获异常[错误:不支持的命令[selectFrame | fraHeader | ]
  4. 插入mydriver.manage()。timeouts()。implicitlyWait(2,TimeUnit.SECONDS);因为代码运行得太快了。这也无济于事。
  5. 这是我的网站代码:

        <meta http-equiv="expires" content="-1">
        <meta http-equiv= "pragma" CONTENT="no-cache">
        <meta http-equiv=Content-Type content="text/html; charset=ISO-8859-1">
        <meta name="robots" content="noindex,nofollow">
        <link rel="P3Pv1" href="/w3c/p3p.xml">
        <script type="text/javascript" src="/scripts/frameset.js"></script>
    </head>
    <frameset id='masterFrameset' rows='130,*,25' border='0' framespacing='0' frameborder='no' onload=''>
        <frame name='fraHeader' noresize scrolling='no' marginwidth='0' marginheight='0' frameborder='no' src='/header-default.jsp'>
        <frame name='fraBody' noresize scrolling='auto' marginwidth='0' marginheight='0' frameborder='no' src='/control/store/login'>
        <frame name='fraFooter' noresize scrolling='no' marginwidth='0' marginheight='0' frameborder='no' src='/footer-default.jsp'>
    </frameset>
    </html>
    &#0149;
    

    我是Selenium的新手并感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

也许您需要使用WebDriverWait等待框架可见。您尝试的是隐式等待,显式等待可能值得尝试。

WebDriverWait wait = new WebDriverWait(driver, 60);  
WebElement iframe = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("masterFrameset"));
driver.switchTo().frame(iframe);

driver.switchTo().frame("masterFrameset");

或iframe是第一个?

driver.switchTo().frame(0);

答案 1 :(得分:2)

除了@nilesh建议的内容外,您还可以使用以下代码等待框架显示并切换到

//Wait for 30 seconds for the frame with name "fraHeader" to appear and then switch to it.
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("fraHeader"));
相关问题