How to select frame if there are multiple frames on a webpage and get content of that frame?

六月ゝ 毕业季﹏ 提交于 2019-12-12 05:39:27

问题


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

  1. 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());
    
  2. 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);
    
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!