How to handle Iframe and nested Iframe and webdriveriframe in WebDriver

Source: Internet
Author: User

How to handle Iframe and nested Iframe and webdriveriframe in WebDriver

Recently, when I used webdriver for crawling, I encountered an iframe problem on the website. After a long time, I did not solve the problem. Later I found that the webdriver had its own processing method, and I felt ashamed ..

1. If iFrame has an ID or name

// Enter the frame id = "frame1" and locate the div and id = "input1" input boxes of id = "div1.

 

Dr. switchTo (). frame ("frame1 ");
Dr. findElement (By. id ("div1 "));
Dr. findElement (By. id ("input1 "))

2. If an iFrame has neither id nor name
// Locate the frame position and select the frame (through xpath)
WebElement frame = driver. findElement (. xpath ("/html/body/div [2]/div [8]/div [2]/div [3]/div [2]/div/iframe ") );
Driver. switchTo (). frame (frame );

3. Jump Out Of iFrame
// Jump out of the frame and enter default content; reposition the id = "id1" div
Dr. switchTo (). defaultContent ();

Dr. findElement (By. id ("id1 "))

 

4. If nested Iframe is processed

Business scenario: locate the "apply for product" drop-down box and select a value for the operation:

 

Shows the page control elements:

 

 

From the information:

 

In the first Iframe, I wrote the application information tab, and in the second Iframe, I wrote the application for product loan usage and so on.

 

 

How can we locate the problem?

One layer to jump into Iframe and exit layer by layer

 


Driver. SwitchTo (). frame ("Cont_right_show");

Driver. SwitchTo (). frame ("AddLoanApp2");

 

Related Article

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.