java - 无法通过Java中的Selenium定位父iframe内的子iframe元素

标签 java selenium selenium-webdriver iframe webdriverwait

到目前为止,这就是我所拥有的:我已将框架切换到其父框架,然后切换到我无法找到的框架:

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&amp;docGroupId=164&amp;connectionPointId=73" style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px;"></iframe>

我试图找到的 iframe 位于多个 div 内。它与错误有什么关系吗?或者我如何找到我的元素有问题?

最佳答案

根据 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')]")));
    

在这里您可以找到关于 Ways to deal with #document under iframe 的相关讨论

关于java - 无法通过Java中的Selenium定位父iframe内的子iframe元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53165931/

相关文章:

java - Hive UDF 将二进制(utf8)转换为 Base64 字符串

python - 我可以在 selenium 运行时最小化 chrome 窗口吗?

Python、Selenium 和 Chromedriver - 使用 find_element_by_id 的无限循环导致 CPU 问题

python-3.x - 在 python 中使用 selenium 禁用 webdriver 的控制台输出

google-chrome - Windows 7 上 ChromeDriver 二进制文件和 Chrome 二进制文件的默认位置

java - 如何处理 Selenium NoSuchElementException

testing - 机器人框架中元素的背景颜色

java - Spring data JPA exists By Field在MySQL中不区分大小写,如何让它区分大小写

JavaMail : Folder. hasNewMessages() 和 .getMessageCount() 似乎无法正常工作

java - 每当发现重复键时如何创建新的 HashMap?