我正在尝试使用动态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&concernRoleID=-8841741561699565568&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&concernRoleID=-8841741561699565568&evidenceType=null"> </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"> </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"> </iframe>
<iframe allowtransparency="true" border="0" class="detailsPanelFrame iframe-ef98a903-a64e-4b67-bba1-46a86eb2569c" data-content-url="en_US/Person_homeTabDetailsPage.do?concernRoleID=-8841741561699565568&o3ctx=36864" frameborder="0" height="99%" page-id="Person_homeTabDetails" scrolling="no" width="99%" src="en_US/Person_homeTabDetailsPage.do?concernRoleID=-8841741561699565568&o3ctx=36864" title="Context Panel - Sharone Stone"> </iframe>
<iframe _srccontents="en_US/Participant_resolveParticipantSmartPanelPage.do?concernRoleID=-8841741561699565568&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&startDate=null&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&startDate=null&calendarViewType=null"> </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);
答案 0 :(得分:1)
您可以使用部分ID或类找到iframe
作为网络元素,并使用它来切换。
看起来src
属性是唯一的。您可以使用它来定位框架。
WebElement iframe = driver.findElement(By.cssSelector("[src*='en_US/Person_homePagePDCPag']"));
driver.switchTo().frame(iframe);
答案 1 :(得分:0)
按顺序选择上下文(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");
}
}