我使用的是IE9 这是我要测试的一些html,在我点击“添加按钮”
后会打开此窗口>DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd"
><html lang="pl" dir="ltr">
> <head>
> <title>SOMETITLE</title>
> <somecode>
> <frameset title="SOMETITLE2" rows="100%,*" onunload="_checkUnload(event)">
> <frame title="SOMETITLE2" src="/somelink&loc=pl" frameBorder="0" noResize="" longDesc="#">
> <!--rce quirks mode for ie-->
> <!--DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" -->
> <html xmlns="http://www.w3.org/1999/xhtml">
> +<head>
> <body class="someBody" onload="return _chain('_checkLoad()','self.setTimeout(\'sizeWin(self,0,0,null)\',100);',this,event)" onunload="return _chain('_checkUnload(event)',' if ((window.event.clientY < 0) || (window.event.clientX < 0)){if(_pprRequestCount==0){if (document.getElementById(\'closeWindow\'))document.getElementById(\'closeWindow\').onclick.call();}}',this,event)">
> <iframe name="_pprIFrame" width="0" height="0" title="" id="_pprIFrame" src="/somelink.gif" frameBorder="0" longDesc="#" style="top: -100px; visibility: hidden; position: absolute;">
> <somecode>
> <table width="100%" border="0" cellSpacing="0" cellPadding="0" summary="">
> <tbody>
> <tr>
> <td class="xd5"/>
> <td>
> <table width="100%" align="center" border="0" cellSpacing="0" cellPadding="0" summary="">
> <div style="top: 0px; position: relative;">
> <table width="790" align="center" style="position: relative;" border="0" cellSpacing="0" cellPadding="0" summary="">
> <tbody>
> <tr>
> <td vAlign="top">
> <table class="x4m" id="_id24" style="display: none;" border="0" cellSpacing="0" cellPadding="0" summary="">
> <form name="frmSearch" id="frmSearch" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;" onkeypress="return _chain('searchOnEnter(event,\'searchButtonHidden\')','return _submitOnEnter(event,\'frmSearch\');',this,event,true)" action="/somelink.faces?_.8" method="post">
> +<div style="display: none; visibility: hidden;">
> <div>
> +<div style="width: 100%; position: relative;">
> <div id="searchCriteria">
> <div style="margin-top: 10px;"/>
> <table class="x4m" id="_id83" style="width: 100%;" border="0" cellSpacing="0" cellPadding="0" summary="">
> <tbody>
> +<tr>
> <td class="x4s"/>
> <td style="vertical-align: top;">
> <div class="x4z" style="width: 100%; height: 100%;">
> <table width="100%" border="0">
> <tbody>
> <tr>
> <td width="50%">
> <table class="xdh" id="name_c_whole" border="0" cellSpacing="0" cellPadding="0" summary="">
> <tbody>
> <tr>
> +<td width="130" class="xdj">
> +<td width="20" class="xdl">
> <td width="50" class="xdm">
> <span class="x1b">
> <input name="name_c" tabIndex="14" class="x1m" id="name_c" style="background-image: none; background-attachment: scroll; background-repeat: repeat; background-position-x: 0%; background-position-y: 0%; background-color: white;" onkeyup=";" type="text" size="40" maxLength="50"/>
> <script>invokeOnchange('name_coperator');</script>
> Text - Empty Text Node
>
>
> <some code>
> <noframes/>
这是我为打开这个框架所写的硒测试
public class wezel_iframe {
private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
System.setProperty("webdriver.ie.driver", "C:/Selenium/IE/IEDriverServer.exe");
baseUrl = "somelink";
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
//capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
capabilities.setCapability("ignoreProtectedModeSettings",true);
capabilities.setCapability("enablePersistentHover", false);
driver = new InternetExplorerDriver(capabilities);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize() ;
}
@Test
public void testNowytestselenium() throws Exception {
WebDriverWait wait = new WebDriverWait(driver, 10);
driver.get(baseUrl + "somelink/login.jsp");
//<somecode>
String parentHandle = driver.getWindowHandle(); // current window handle
driver.findElement(By.id("function_add_button_id")).click(); // opens a new window
Thread.sleep(3000);
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle); // switch focus to newly opened window
}
driver.manage().window().maximize() ;
//window is maximasing so it works fine
driver.switchTo().frame(driver.findElement(By.cssSelector("frame[title='Zawartość']"))); //no exception -sims to work ok
driver.switchTo().frame(driver.findElement(By.id("_pprIFrame")));//no exception -sims to work ok
driver.findElement(By.id("name_c")).sendKeys("CMK"); // NoSuchElementException
driver.close();
driver.switchTo().window(parentHandle);
问题是我不能对这个框架内的对象做任何事情,不管我做什么我总是得到NoSuchElementException
答案 0 :(得分:0)
尝试进行一些等待,因为即使在加载网页后iframe也需要时间加载。
明确等待如下:
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("_pprIFrame"));