【问题标题】:Selenium Web Driver - Not able to switch to Iframe using dynamic iframe idSelenium Web 驱动程序 - 无法使用动态 iframe id 切换到 iframe
【发布时间】:2016-08-17 13:01:16
【问题描述】:

我正在尝试使用动态 iframe id 切换到 iframe。但是,错误如下。任何帮助将不胜感激。

org.openqa.selenium.NoSuchFrameException: No frame element found by name or id iframe-8c590576-9047-4baf-a34f-69fe67a295a1

以下是我想要切换的第一个 iframe 中的 HTML:

<iframe allowtransparency="true" class="iframe-4be2b789-7f13-4350-9771-0523258c080e contentPanelFrame" data-content-url="en_US/Person_homePagePDCPage.do?o3ctx=4096&amp;concernRoleID=-8841741561699565568&amp;evidenceType=null" data-dojo-attach-point="frame" frameborder="0" id="iframe-4be2b789-7f13-4350-9771-0523258c080e" iscpiframe="true" marginheight="0" marginwidth="0" title="Content Panel - Home" src="en_US/Person_homePagePDCPage.do?o3ctx=4096&amp;concernRoleID=-8841741561699565568&amp;evidenceType=null">&nbsp;</iframe>




<iframe allowtransparency="true" class="iframe-8e82cd97-893d-41ec-b2e5-c6151d6a34a6 contentPanelFrame" data-content-url="en_US/SCSysMAAdminHomePagePage.do?o3ctx=4096" data-dojo-attach-point="frame" frameborder="0" id="iframe-8e82cd97-893d-41ec-b2e5-c6151d6a34a6" iscpiframe="true" marginheight="0" marginwidth="0" title="Content Panel - Home" src="en_US/SCSysMAAdminHomePagePage.do?o3ctx=4096" cd_frame_id_="146a47c04d9f04d598ccfa3f99c1674d">&nbsp;</iframe>



<iframe allowtransparency="true" class="iframe-f41ba74a-5362-41f5-977f-e144c8c7e4e2 contentPanelFrame" data-content-url="en_US/Person_search1Page.do?o3ctx=4096" data-dojo-attach-point="frame" frameborder="0" id="iframe-f41ba74a-5362-41f5-977f-e144c8c7e4e2" iscpiframe="true" marginheight="0" marginwidth="0" title="Content Panel - Home" src="en_US/Person_search1Page.do?o3ctx=4096">&nbsp;</iframe>



<iframe allowtransparency="true" border="0" class="detailsPanelFrame iframe-ef98a903-a64e-4b67-bba1-46a86eb2569c" data-content-url="en_US/Person_homeTabDetailsPage.do?concernRoleID=-8841741561699565568&amp;o3ctx=36864" frameborder="0" height="99%" page-id="Person_homeTabDetails" scrolling="no" width="99%" src="en_US/Person_homeTabDetailsPage.do?concernRoleID=-8841741561699565568&amp;o3ctx=36864" title="Context Panel - Sharone Stone"> </iframe>



<iframe _srccontents="en_US/Participant_resolveParticipantSmartPanelPage.do?concernRoleID=-8841741561699565568&amp;o3ctx=266240" allowtransparency="true" border="0" class="smartPanelFrame iframe-f9201550-6ac4-4916-9262-988c17c9a24d" frameborder="0" height="99%" id="curam_tab_SmartPanelIframe" iframeloaded="false" name="curam_tab_SmartPanelIframe" page-id="Participant_resolveParticipantSmartPanel" scrolling="no" src="" title="Smart Panel" width="99%"> </iframe>



<iframe allowtransparency="true" class="iframe-7c91f6ef-a3d3-460e-83b5-d405503921e3 contentPanelFrame" data-content-url="en_US/Activity_redirectCurrentUserCalendarPage.do?o3ctx=4096&amp;startDate=null&amp;calendarViewType=null" data-dojo-attach-point="frame" frameborder="0" id="iframe-7c91f6ef-a3d3-460e-83b5-d405503921e3" iscpiframe="true" marginheight="0" marginwidth="0" title="Content Panel - Home" src="en_US/Activity_redirectCurrentUserCalendarPage.do?o3ctx=4096&amp;startDate=null&amp;calendarViewType=null">&nbsp;</iframe>



<iframe id="ie-progress-indicator-helper" name="null" src="about:blank" onload="null" style="position: absolute; left: 1px; top: 1px; height: 1px; width: 1px; visibility: hidden" title="ie-progress-indicator-helper"></iframe>



<iframe id="ie-progress-indicator-helper" name="null" src="about:blank" onload="null" style="position: absolute; left: 1px; top: 1px; height: 1px; width: 1px; visibility: hidden" title="ie-progress-indicator-helper"></iframe>



<iframe id="ie-progress-indicator-helper" name="null" src="about:blank" onload="null" style="position: absolute; left: 1px; top: 1px; height: 1px; width: 1px; visibility: hidden" title="ie-progress-indicator-helper"></iframe>

以下是我的代码:

driver.switchTo().frame("iframe-8c590576-9047-4baf-a34f-69fe67a295a1");
            Thread.sleep(2000);

我尝试使用下面的 xpath,但没有运气:

driver.findElement(By.xpath("//html/body/div[1]/div[4]/div[3]/div[2]/div[3]/div[3]/div[2]/div/div[4]/div/div/div[1]/div/div[3]/div/div[2]/iframe/html/body/div[2]/div[6]/div/table/tbody/tr/td[1]/div/div/table/tbody/tr/td[1]/[contains(@class, 'field-link') and contains(@class ,'external-link')]")).click();
            Thread.sleep(2000);

【问题讨论】:

  • html中没有&lt;iframe&gt;
  • 嗨,我更新了 HTML 中的 iframe id。
  • 这样的页面只有一个iframe还是多个iframe??
  • 嗨 Saurabh Gaur - 我看到了 HTML,并在那里找到了多个 iframe id。

标签: java selenium-webdriver jxl


【解决方案1】:

您可以使用部分 id 或类将 iframe 定位为 Web 元素并使用它进行切换。

看起来src 属性是唯一的。您可以使用它来定位框架。

WebElement iframe = driver.findElement(By.cssSelector("[src*='en_US/Person_homePagePDCPag']"));
driver.switchTo().frame(iframe);

【讨论】:

  • 嗨,我在想要切换到特定 iframe 的页面上发现了多个具有动态 id 的 iframe 所以在这种情况下,我们创建的对象可能会寻找另一个 iframe?
  • @Curious 你能发布另一个框架的 html 吗?这个想法是识别目标框架,任何唯一的选择器都可以。
  • 嗨,我更新了上面的所有 iframe HTML,并从我要切换的第一个 iframe 中删除。
  • 嗨,伙计 - 我按照你的建议更新了我的代码,这次它对我有用,可以切换我想要的 iframe。非常感谢!!
【解决方案2】:

通过排序选择上下文(iframe)是一种不好的做法,但如果没有其他方法来识别它们,您可以使用以下示例。

public void fillStripeTextInputWithNameAndValue(String name,String value) 


throws Exception{

           switch (name) {
               case "cardnumber":

                   driver.findElement(By.xpath("//iframe[@name='__privateStripeFrame4']")).sendKeys(value);
                   break;


               case "exp-date":
                   driver.findElement(By.xpath("//iframe[@name='__privateStripeFrame5']")).sendKeys(value);
                  break;


               case "cvc":
                   driver.findElement(By.xpath("//iframe[@name='__privateStripeFrame6']")).sendKeys(value);
                   break;


                   default:
                       throw  new Exception("Unknown stripe input field");
           }
       }

【讨论】:

    猜你喜欢
    • 2021-06-19
    • 1970-01-01
    • 2017-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-06
    • 2017-12-24
    • 1970-01-01
    相关资源
    最近更新 更多