Selenium Webdriver Learning (v) Processing of------------iframe (RPM)

Source: Internet
Author: User

Selenium Webdriver Study (v) Treatment of------------iframe Blog Category:
    • Selenium-webdriver
How to position elements in a frame

Sometimes when we locate a page element, we find that we can't locate it, and check the locator that we wrote repeatedly without any problems, and the code doesn't have any problems. At this point you will have to look at whether this page element is in an IFRAME, which may be one of the reasons why you cannot find it. If you are looking for an element in the IFRAME in a default content, it must not be found. Conversely, if you look for another IFRAME element in an IFRAME or an element in the default content, it must not be located.

The selenium Webdriver provides a way to enter an IFRAME:

Webdriver Org.openqa.selenium.WebDriver.TargetLocator.frame (String nameorid)

It also provides a way to return the default content:

Webdriver Org.openqa.selenium.WebDriver.TargetLocator.defaultContent ()

This makes it easy for us to deal with the IFRAME.

Take the following HTML code as an example, and let's look at a current IFRAME.

HTML code
  1. Main.html
  2. <html>
  3. <head>
  4. <title>frametest</title>
  5. </head>
  6. <body>
  7.     <div id  =  "ID1" >this is a div! </div>  
  8. <iframe id = "frame" frameborder="0" scrolling= "no" style="left:0; Position:absolute; " src = "frame.html"></iframe>
  9. </Body>
  10. </html>
  11. Frame.html
  12. <html>
  13. <head>
  14. <title>this is a frame! </title>
  15. </head>
  16. <body>
  17. <div id = "div1">this is a div,too! </div>
  18. <label>input:</label>
  19. <input id = "input1"></input>
  20. </Body>
  21. </html>

Java code
  1. Import Org.openqa.selenium.By;
  2. Import Org.openqa.selenium.WebDriver;
  3. Import Org.openqa.selenium.firefox.FirefoxDriver;
  4. Public class Famestudy {
  5. public static void Main (string[] args) {
  6. Webdriver dr = new Firefoxdriver ();
  7. String url = "\\Your\\Path\\to\\main.html";
  8. Dr.get (URL);
  9. //In the default content location id= "Id1" div
  10. Dr.findelement (By.id ("ID1"));
  11. //At this time, do not enter into the id= "frame" frame, the following two sentences will be error
  12. Dr.findelement (By.id ("Div1")); Error
  13. Dr.findelement (By.id ("input1")); Error
  14. //Enter the frame of id= "frame" to locate the id= "Div1" div and the input box id= "INPUT1".
  15. Dr.switchto (). FRAME ("frame");
  16. Dr.findelement (By.id ("Div1"));
  17. Dr.findelement (By.id ("input1"));
  18. //At this point, if you do not jump out of frame, you will get an error if you locate the element in default content.
  19. Dr.findelement (By.id ("ID1")); Error
  20. //Jump out of frame, go to default content, reposition id= "Id1" div
  21. Dr.switchto (). Defaultcontent ();
  22. Dr.findelement (By.id ("ID1"));
  23. }
  24. }

The switch_to method new1 A Targetlocator object, using the object's frame method to move the currently recognized "body" to the frame that needs to be positioned.

Selenium Webdriver Learning (v) Processing of------------iframe (RPM)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.