Throw "No such element" after perform switch to parent iFrame
Not reproducible Issue #14556985
Steps to reproduce
Hi,
I’m using selenium and I’d like find a web element in a iframe but failed. For further detail please run java code below:
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.util.List;
public class TestClickFF {
public static void main(String [ ] args) throws Exception{
// // EDGE
System.setProperty("webdriver.edge.driver", “D:\selenium\web driver\edge\MicrosoftWebDriver_15063.exe”);
DesiredCapabilities edgeCapabilities = DesiredCapabilities.edge();
WebDriver webDriver = new EdgeDriver(edgeCapabilities);
// CHROME
// System.setProperty("webdriver.chrome.driver", “D:\selenium\web driver\chrome\chromedriver_win32_2.33.exe”);
// ChromeOptions chromeOptions = new ChromeOptions();
// WebDriver webDriver = new ChromeDriver(chromeOptions);
// goto tutorialspoint.com site
webDriver.get("https://www.tutorialspoint.com/javascript/javascript_events.htm");
// get the element on iframe
WebElement webElement = null;
try {
webElement = webDriver.findElement(By.xpath("//input[@type='button']"));
} catch (Exception e) {
System.out.println(e.getMessage());
}
if (webElement == null) {
String urlCurrent = webDriver.getCurrentUrl();
List<WebElement> webElementList = findElementInWholePage(webDriver, urlCurrent);
if (webElementList != null && webElementList.size() > 0) {
webElement = webElementList.get(0);
}
}
// click element but it not perform "onclick" event that set on element
if (webElement != null) {
webElement.click();
System.out.println("Done!");
} else {
System.err.println("Element not found.");
}
Thread.sleep(5000);
webDriver.quit();
}
private static List<WebElement> findElementInWholePage(WebDriver wdDriver, String urlFindStart) throws InterruptedException {
List<WebElement> lsEle = null;
try {
// Get all FRAMES from selected page
List<WebElement> lsIFrames = wdDriver.findElements(By.xpath("//iframe"));
boolean isSwitchedToIframe = false;
// Iterate through FRAMES (recursive loop)
for (WebElement weIFrame : lsIFrames) {
if (lsEle == null || lsEle.size() == 0) {
try {
isSwitchedToIframe = false;
if (!weIFrame.isDisplayed() || weIFrame.getSize().getWidth() <= 1 || weIFrame.getSize().getHeight() <= 1) {
continue;
}
wdDriver.switchTo().frame(weIFrame);
isSwitchedToIframe = true;
} catch (WebDriverException e) {
System.out.printf(e.getMessage());
} catch (Exception ex) {
System.out.printf("Exception: " + ex);
}
// Search element by XPATH
lsEle = wdDriver.findElements(By.xpath("//input[@type='button']"));
if (!lsEle.isEmpty()) {
return lsEle;
} else {
// Perform search on next FRAME
lsEle = findElementInWholePage(wdDriver, urlFindStart);
if (isSwitchedToIframe) {
switchToParentFrame(wdDriver, urlFindStart);
}
}
} else {
return lsEle;
}
}
} catch (StaleElementReferenceException e) {
System.out.println(e.getMessage());
}
return lsEle;
}
// Back to parent frame
private static void switchToParentFrame(WebDriver webDriver, String urlFindStart) {
try {
String urlCurrent = webDriver.getCurrentUrl();
if (urlCurrent.equals(urlFindStart)) {
webDriver.switchTo().parentFrame();
}
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
You need to sign in to your Microsoft account to add a comment.
Sign in