【问题标题】:Selenium find element nested in iframe>frameset>frameset>frame [duplicate]Selenium 查找嵌套在 iframe>frameset>frameset>frame 中的元素 [重复]
【发布时间】:2019-05-02 16:39:01
【问题描述】:

这是一个关于 Selenium 的问题......但也是 JS!
所以请不要以 JS 以外的任何其他语言发布答案。
我尝试翻译其他语言的答案但失败了。如果您知道可以自动将 Selenium 代码转换为 JS 等效代码并可以工作的工具,请告诉我。

我想点击一个带有 selenium 的项目(selenium-webdriver,使用 nodejs)但是当我通过 id 搜索它时找不到所需的元素。

我尝试了很多不同的事情,比如切换框架,但这种嵌套真的让我很受用。

我将大 html 页面简化为基础:
- 我的目标是选择a-tag id 为action

<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <title>Selenium Test</title>
  </head>
  <body>
    <div id="mainDiv">
      <div id="appFrameContainer">
        <iframe name="appFrame" id="appFrame" src="/longlink">
          #document
          <!DOCTYPE HTML>
          <html>
            <head></head>
            <frameset>
              <frameset id="frameset2">
                <frame src="/longlink2" name="frame1" id="frame1">
                #document
                <!DOCTYPE HTML>
                <html xmlns="http://www.w3.org/1999/xhtml">
                  <head></head>
                  <body>
                    <div id="menu">
                      <table class="table">
                        <tbody>
                          <tr>
                            <td>
                              <div>
                                <ul>
                                  <li id="item">
                                    <span>
                                      <a id="action"> 
                                        Execute Action
                                      </a>
                                    </span>
                                  </li>
                                </ul>
                              </div>
                            </td>
                          </tr>
                        </tbody>
                      </table>
                    </div>
                  </body>
                </html>
                </frame>
              </frameset>
            </frameset>
          </html>
        </iframe>
      </div>
    </div>
  </body>
</html>

(不幸的是,当我将该文本粘贴到 html 文件中作为缩小示例时,第二个 body-tag 保持为空,我不知道为什么。)

我现在正在尝试这样的事情并收到错误:
UnhandledPromiseRejectionWarning: InvalidArgumentError: data did not match any variant of untagged enum FrameId at line 1 column 28

//Setup
const Capabilities = require('selenium-webdriver/lib/capabilities').Capabilities;
let capabilities = Capabilities.firefox();
capabilities.set('acceptInsecureCerts', true);
const firefox = 'firefox';
let driver = new Builder()
    .forBrowser(firefox)
    .withCapabilities(capabilities)
    .build();
driver.get("<url>").then(startAutomation).catch();

//Process
async function startAutomation() {
    let mainFrameName = "appFrame";
    let idAction = "action";

    driver.switchTo().frame(mainFrameName);
    driver.findElement(By.id(idAction)).then(e => {
         e.click();
    });
}

【问题讨论】:

  • 你检查过我的答案Finding Element in multiple Iframe
  • 我会尝试,但在我的情况下,我也有框架集和框架,它们是嵌套的,不知道如何从该答案更改您的代码以使其工作跨度>
  • @supputuri 我尝试了很多东西,但我无法将您的代码翻译成 nodejs - 出现大量错误

标签: javascript html node.js selenium


【解决方案1】:

您能否检查以下代码是否适合您(应单击具有 id="action" 的锚标记)

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("document.getElementById('appFrame').contentWindow.document.getElementById('action').click();")

【讨论】:

  • 您发布了 Java 代码,我的问题是 JS,我不知道如何翻译您的代码以在我的 nodejs 服务器上运行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-28
  • 2022-11-07
  • 1970-01-01
相关资源
最近更新 更多