2012-03-30 2 views
26
<div>  
    <iframe id="cq-cf-frame ">  
    <iframe id="gen367"> 
     <body spellcheck="false" id="CQrte" style="height: 255px; font-size: 12px; font-family:tahoma,arial,helvetica,sans-serif; background-image: url(&quot;/libs/cq/ui/widgets/themes/default/ext/form/text-bg.gif&quot;); background-repeat: repeat-x; background-attachment: fixed;"> 
     <p>4t43t4<br></p> 
     </body > 
    </iframe> 
    </iframe>  
</div> 

In questo scenario c'è un iframe sotto iframe. E devo selezionare l'esterno iframe per andare al numero interno iframe e scrivere nel corpo che si trova nel numero interno iframe.Come gestire iframe in selenio WebDriver utilizzando Java

Successivamente, devo uscire dall'interno iframe all'esterno iframe e fare clic sul pulsante OK, (che si trova nell'esterno iframe).

seguito è il mio codice

/*Line 1 */ driver.switchTo().frame("cq-cf-frame"); 
/*  2 */ driver.findElement(By.css("#extdd-9 > div.tblRow > input.edititem").click(); 
/*  3 */ driver.switchT().Frame("cq-gen379"); 
/*  4 */ driver.findElement(By.id("CQrte").sendKeys("Tnx"); 
/*  5 */ selenium.selectFrame("relative=up");  
/*  6 */ driver.findElement(By.xpath("//button[text()='OK']")).click(); 

seguito è il mio problema:

Il mio codice di prova sta lavorando bene fino alla linea numero 4 cioè scrivendo nel corpo, ma voglio venire da interno a esterno iframe dice che l'elemento //button[text()='OK'] non trovato.

Ho provato con l'utilizzo dell'indice, genitore, relativo, ma non ho avuto fortuna.

NOTA: Se non si seleziona frame interno (cq-gen379). Sono in grado di fare clic sul pulsante OK.

risposta

36

In WebDriver, è necessario utilizzare driver.switchTo().defaultContent(); per uscire da una cornice. Prima è necessario uscire da tutti i fotogrammi, quindi passare nuovamente alla cornice esterna.

// between step 4 and step 5 
// remove selenium.selectFrame("relative=up"); 
driver.switchTo().defaultContent(); // you are now outside both frames 
driver.switchTo().frame("cq-cf-frame"); 
// now continue step 6 
driver.findElement(By.xpath("//button[text()='OK']")).click(); 
+0

Inoltre, come una FYI: ho trovato che il frame switchTo(). Ha bisogno di attesa implicita disattivata: driver.manage(). Timeouts(). ImplicitlyWait (0, TimeUnit.SECONDS); – JohnP2

30

Dovete ottenere di nuovo fuori dalla Iframe con il seguente codice:

driver.switchTo().frame(driver.findElement(By.id("frameId"))); 
//do your stuff 
driver.switchTo().defaultContent(); 

speranza che aiuta

+0

Sono d'accordo su questa risposta. Nella sezione "// fai la tua roba" utilizzerei JavaScriptExectutor se il DOM restituito sembra essere nullo. D: Perché il metodo chiama ".frame" quando il nome del tag attuale è "iframe" ??? I frame/frameset non sono diversi da un iframe? – djangofan

+0

Ciao, questa è solo una mia ipotesi, ma un iframe è ancora una sorta di frame e questo pezzo di codice non è esclusivo di iframe, quindi dovrebbe funzionare con altri frame (non l'ho verificato) – Tarken

+0

Se si salva l'url con 'driver. getCurrentUrl(). then (function (data) { \t console.log ("l'url è:" + data); }); "troverai che si tratta di un percorso diverso senza alcune funzionalità. In pratica hai lasciato tutti gli script. Sto ancora cercando di capirlo da solo. – Shane

1

per tornare al telaio genitore, uso:

driver.switchTo().parentFrame(); 

per tornare al primo fotogramma principale /, uso:

driver.switchTo().defaultContent(); 
17

Hai bisogno di trovare prima iframe. Puoi farlo usando la seguente dichiarazione.

WebElement iFrame= driver.findElement(By.tagName("iframe")); 

Quindi, è possibile swith ad esso utilizzando switchTo metodo su voi WebDriver oggetto.

driver.switchTo().frame(iFrame); 

E per tornare al frame genitore, è possibile utilizzare switchTo().parentFrame() o se si vuole tornare al telaio principale (o più genitori), è possibile utilizzare switchTo().defaultContent();.

driver.switchTo().parentFrame(); // to move back to parent frame 
driver.switchTo().defaultContent(); // to move back to most parent or main frame 

Spero che sia d'aiuto.

2
WebDriver driver=new FirefoxDriver(); 
driver.get("http://www.java-examples.com/java-string-examples"); 
Thread.sleep(3000); 
//Switch to nested frame 
driver.switchTo().frame("aswift_2").switchTo().frame("google_ads_frame3"); 
1

Di seguito l'approccio di gestione telaio: Quando nessun id o il nome è dato in caso di telaio nidificato

WebElement element =driver.findElement(By.xpath(".//*[@id='block-block19']//iframe")); 
driver.switchTo().frame(element); 
driver.findElement(By.xpath(".//[@id='carousel']/li/div/div[3]/a")).click(); 
0

Selenio Web driver Frames Handling

driver.switchTo().defaultContent(); [parentFrame, defaultContent, frame]

Frames :

// Based on index position: 
int frameIndex = 0; 
List<WebElement> listFrames = driver.findElements(By.tagName("iframe")); 
System.out.println("list frames "+listFrames.size()); 
driver.switchTo().frame(listFrames.get(frameIndex)); 

// XPath|CssPath Element: 
WebElement frameCSSPath = driver.findElement(By.cssSelector("iframe[title='Fill Quote']")); 
WebElement frameXPath = driver.findElement(By.xpath(".//iframe[1]")); 
WebElement frameTag = driver.findElement(By.tagName("iframe")); 

driver.switchTo().frame(frameCSSPath); // frameXPath, frameTag 


driver.switchTo().frame("relative=up"); // focus to parent frame. 
driver.switchTo().defaultContent(); // move to the most parent or main frame 

// For alert's 
Alert alert = driver.switchTo().alert(); // Switch to alert pop-up 
alert.accept(); 
alert.dismiss(); 

test XML:

<html> 
    <IFame id='1'>...  parentFrame() « context remains unchanged. <IFame1> 
    | 
    -> <IFrame id='2'>... parentFrame() « Change focus to the parent context. <IFame1> 

    <Fame id='11'>...  defaultContent() « driver focus to top window/first frame. <html> 
    | 
    -> <Frame id='22'>... defaultContent() « driver focus to top window/first frame. <Fame11> 
          frame("relative=up") « focus to parent frame. <Fame11> 

    ... 

Conversione di RC per Web-driver Java comandi. link.