2015-09-27 51 views
0

在我的Java应用程序,我想切换到WordPress的IFRAME content_ifr用Java代码:硒PhantomJSDriver - 无法切换到帧

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

但不断得到一个以下异常:

org.openqa.selenium.NoSuchFrameException: {"errorMessage":"Unable to switch to frame","request":{"headers":{"Accept":"application/json, image/png","Connection":"Keep-Alive","Content-Length":"20","Content-Type":"application/json; charset=utf-8","Host":"localhost:15910"},"httpVersion":"1.1","method":"POST","post":"{\"id\":\"content_ifr\"}","url":"/frame","urlParsed":{"anchor":"","query":"","file":"frame","directory":"/","path":"/frame","relative":"/frame","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/frame","queryKey":{},"chunks":["frame"]},"urlOriginal":"/session/d2c0de40-6349-16e5-8bdf-f3a5c98d0edc/frame"}} 

驱动程序信息:

Driver info: org.openqa.selenium.phantomjs.PhantomJSDriver 
Capabilities [{applicationCacheEnabled=false, rotatable=false, handlesAlerts=false, databaseEnabled=false, version=1.9.8, platform=XP, browserConnectionEnabled=false, proxy={proxyType=direct}, nativeEvents=true, acceptSslCerts=false, driverVersion=1.1.0, locationContextEnabled=false, webStorageEnabled=false, browserName=phantomjs, takesScreenshot=true, driverName=ghostdriver, javascriptEnabled=true, cssSelectorsEnabled=true}] 

我使用:

selenium 2.47.2 
phantomjs-1.9.8-windows 

什么可能是错误的?

回答

0

我实现了一个肮脏的黑客:

List<WebElement> elements = driver.findElements(By.tagName("iframe")); 
WebElement iframe = null; 
for (int i = 0; i < elements.size(); i++) { 
    if("content_ifr".equals(elements.get(i).getAttribute("id"))) { 
     iframe = elements.get(i); 
     break; 
    } 
} 
driver.switchTo().frame(iframe); 

现在的作品..

+0

为什么不'driver.switchTo()框架(driver.findElement(By.id( “content_ifr”)) )'?此外,你可以检查页面上是否有两个元素与该ID?如果是这样,iframe是否是第一个? –

+0

仍然无法正常工作..有时content_ifr无法找到某种原因.. – alexanoid

+0

你可能需要一个明确的等待,等待的iframe被放入页面。 –