问题
So far, this is what I have: I've switched the frame to its parent frame then to the the frame I am unable to locate:
By frame = By.xpath("//iframe[@class='GenCss_style-Model']");
driver.switchTo().frame(driver.findElement(By.name("documentflowdesk")));
driver.switchTo().frame(driver.findElement(frame));
Since 'frame' element is not found, I get this error:
org.openqa.selenium.NoSuchElementException: Unable to locate element
HTML:
<iframe src="about:blank" name="documentflowdesk" class="gwt-Frame-NavigationComponentViewImplResourcesapplicationFrame" id="documentflowdesk" style="position: absolute; left: 0px; top: 0px; visibility: visible; height: 100%;"></iframe>
#documentflowdesk
<html style="overflow: hidden;">
<head>...</head>
<body style="margin: 0px;" class="dragdrop-dropTarget dragdrop-boundary">
<noscript>...</noscript>
<div style="position: absolute; overflow: hidden; left: 0px; top: 0px; right: 0px; bottom: 0px;">
<div class="css-DeskStyleResources" style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px;">
<div style="position: absolute; overflow: hidden; left: 0px; top: 0px; right: 0px; bottom: 0px;">
<div style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px;">
<div style="position: absolute; overflow: hidden; left: 0px; top: 0px; right: 0px; bottom: 0px;">
<iframe class="GenCss_style-Model" src="/model/?modelId=100&docGroupId=164&connectionPointId=73" style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px;"></iframe>
The iframe I'm trying to locate is inside multiple div. Does it have to do anything with the error? Or is there a problem with how I find my element?
回答1:
As per the HTML once you are on the parent frame you have to:
- Induce WebDriverWait for the parent frame to be available and switch to it.
- Induce WebDriverWait for the child frame to be available and switch to it.
You can use the following solution:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@class='gwt-Frame-NavigationComponentViewImplResourcesapplicationFrame' and @id='documentflowdesk']"))); new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@class='GenCss_style-Model' and contains(@src,'connectionPointId')]")));
Here you can find a relevant discussion on Ways to deal with #document under iframe
回答2:
Please try below code:
driver.switchTo().frame(driver.findelement(By.xpath(//iframe[@name='documentflowdesk']);
Thread.sleep(5000);
driver.switchTo().frame(driver.findelement(By.xpath(//ifame[@class='GenCss_style-Model']);
来源:https://stackoverflow.com/questions/53165931/unable-to-locate-the-child-iframe-element-which-is-within-the-parent-iframe-thro