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");