Java 中的 Selenium 无法定位父 iframe 中的子 iframe 元素
Unable to locate the child iframe element which is within the parent iframe through Selenium in Java
到目前为止,这就是我所拥有的:我已将框架切换到其父框架,然后切换到我无法定位的框架:
By frame = By.xpath("//iframe[@class='GenCss_style-Model']");
driver.switchTo().frame(driver.findElement(By.name("documentflowdesk")));
driver.switchTo().frame(driver.findElement(frame));
由于未找到 'frame' 元素,我收到此错误:
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>
我要查找的 iframe 在多个 div 中。它与错误有什么关系吗?或者我如何找到我的元素有问题?
请尝试以下代码:
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']);
根据 HTML 一旦你进入 父框架 你必须:
- 为父框架引入WebDriverWait并切换到它。
- 为子框架引入WebDriverWait并切换到它。
您可以使用以下解决方案:
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')]")));
在这里你可以找到关于
的相关讨论
到目前为止,这就是我所拥有的:我已将框架切换到其父框架,然后切换到我无法定位的框架:
By frame = By.xpath("//iframe[@class='GenCss_style-Model']");
driver.switchTo().frame(driver.findElement(By.name("documentflowdesk")));
driver.switchTo().frame(driver.findElement(frame));
由于未找到 'frame' 元素,我收到此错误:
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>
我要查找的 iframe 在多个 div 中。它与错误有什么关系吗?或者我如何找到我的元素有问题?
请尝试以下代码:
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']);
根据 HTML 一旦你进入 父框架 你必须:
- 为父框架引入WebDriverWait并切换到它。
- 为子框架引入WebDriverWait并切换到它。
您可以使用以下解决方案:
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')]")));
在这里你可以找到关于