问题
I am trying to get the content of the email from site name "yopmail.com" which is having multiple frames. I need to switch to the frame name = 'ifmail' and want to getText from email body.
Please see screenshot for yopmail:
Anyone can help me out here?
回答1:
i use the below code to get the text of email body:
Thread.sleep(4000);
driver.switchTo().frame("ifmail");
List<WebElement> elems = driver.findElements(By.cssSelector("div#mailmillieu>div>div[dir='ltr']>div"));
for(WebElement element: elems){
if(!element.getText().equals("")){
System.out.println("body is "+element.getText());
}
}
i use "" as there are some br and new line in my body. But it will show all the text of ur body.
回答2:
You can give the name as parameter to the switch command
driver.switchTo().frame("ifmail");
回答3:
Hi please handle iframe like below
driver.switchTo().frame(int arg0); // Select a frame by its (zero-based) index. That is, if a page has multiple frames (more than 1), the first frame would be at index "0", the second at index "1" and so on.
List<WebElement> iframeElements = driver.findElements(By.tagName("iframe"));System.out.println("The total number of iframes are " + iframeElements.size());driver.switchTo().frame(String arg0); // Below is the example code snippet using frame a.Name. b.Id. c.WebElement
driver.switchTo().frame(frame); System.out.println("Navigated to frame with name " + frame);Switching back to Main page from Frame
driver.switchTo().defaultContent();
Hope this helps you
回答4:
You can switch to frame by name, id, webelement , index
Here the HTML of frame:-
<iframe class="whc" frameborder="0" scrolling="auto" id="ifmail" name="ifmail" src=""></iframe>
You id and name have the same name so you can switch it as below :-
driver.switchTo().frame("ifmail");
OR
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@id='ifmail']")));
Hope it will help you :)
回答5:
Please try as below, hope it helps:
//Switch to Main page first
driver.switchTo().defaultContent();
//Switch to the desired frame
driver.switchTo().frame(frame);
// perform steps you want to do
//and then Switching to Main page again
driver.switchTo().defaultContent();
来源:https://stackoverflow.com/questions/36996195/how-to-select-frame-if-there-are-multiple-frames-on-a-webpage-and-get-content-of