首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iframe>frameset>frameset>frame中嵌套的Selenium查找元素

iframe>frameset>frameset>frame中嵌套的Selenium查找元素
EN

Stack Overflow用户
提问于 2019-05-02 16:39:01
回答 1查看 626关注 0票数 1

这是一个关于Selenium...but的问题,也是JS!

因此,除了JS之外,请不要用任何其他语言发布答案。

我试着翻译其他语言的答案,但都失败了。如果您知道一个工具可以自动将Selenium代码转换为JS等价物并工作,请告诉我。

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

我尝试了很多不同的东西,比如切换帧,但是这种嵌套确实让我得到了最好的结果。

我把html的大页面简化到了基本内容:

  • 我的目标是使用id action选择a-tag
代码语言:javascript
复制
<!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

代码语言:javascript
复制
//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();
    });
}
EN

回答 1

Stack Overflow用户

发布于 2019-05-02 18:01:43

请您检查下面的代码是否适合您(请单击具有id=“action”的锚标记)

代码语言:javascript
复制
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("document.getElementById('appFrame').contentWindow.document.getElementById('action').click();")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55956818

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档