java selenium handles elements in Iframe
Sometimes when we locate elements, we find that we can't locate them no matter what. At this time, you need to check whether the element you want to locate is in the iframe
Reading Contents
What is an iframe
iframe is used in HTML for web pages to nest web pages. One web page can be nested into another web page, and can be nested with many layers.
The selenium provides a method to enter an iframe
// Enter iframedr.switchTo().frame("frameA");// Return to the main window dr.switchTo().defaultContent(); main.html
<html><head> <title>FrameTest</title></head><body> <div id="id1">this is main page's div!</div> <input type="text" id="maininput" /> <br/> <iframe id="frameA" frameborder="0" scrolling="no" style="left:0;position:absolute;" src="frame.html"></iframe></body></html>
frame.html
<html><head> <title>this is a frame!</title></head><body> <div id="div1">this is iframes div,</div> <input id="iframeinput"></input></body></html>
selenium code
public static void testIframe(WebDriver driver) { driver.get("E://StashFolder//[email protected]//Stash//Tank-MoneyProject//Pudong Software Park Training Center//My Textbook//Selenium Webdriver//frame//main.html"); // Driver.findElement(By.id("maininput")).sendKeys("main input"); // At this time, the iframe is not entered, The following statement will report an error //driver.findElement(By.id("iframeinput")).sendKeys("iframe input"); driver.switchTo().frame("frameA"); driver.findElement(By.id("iframeinput")).sendKeys("iframe input"); // It is not in the main window at this time, and the following statement will report an error //driver.findElement(By.id("maininput")).sendKeys("main input"); // Return to the main window driver.switchTo().defaultContent(); driver.findElement(By.id("maininput")).sendKeys("main input"); }The above is an example of Java selenium processing elements in Iframe. We will continue to organize relevant information in the future. Thank you for your support for this site!