1

This is a question regarding Selenium...but also JS!
So please don't post answers in any other languages than JS.
I tried and failed to translate answers from other languages. If you know a tool that can automatically translate Selenium code to the JS equivalent and work, let me know.

I want to click on an item with selenium (selenium-webdriver, using nodejs) but it cannot find the desired element when I search for it by id.

I tried many different things like switching frames, but this kind of nesting really got the best of me.

I stripped the big html page down to the basics:
- my goal is selecting the a-tag with the 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>

(Unfortunately when I paste that text into an html file as a minified example the second body-tag stays empty, I don't know why.)

I'm trying something like this right now and getting the error:
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();
    });
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Cold_Class
  • 3,214
  • 4
  • 39
  • 82
  • Have you checked my answer [Finding Element in multiple Iframe](https://stackoverflow.com/questions/55536851/is-there-a-way-to-search-webelement-on-a-main-window-first-if-not-found-then-s/55537186#comment97778812_55537186) – supputuri May 02 '19 at 16:51
  • I will try, but in my case I have framesets and frames as well, and they're nested, not sure how I'd have to change your code from that answer to make it work – Cold_Class May 02 '19 at 17:07
  • @supputuri I tried many things, but I can't get your code translated to nodejs - getting tons of errors – Cold_Class May 06 '19 at 15:26

1 Answers1

0

Can you please check if below code works for you (shall click on anchor tag having id="action")

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("document.getElementById('appFrame').contentWindow.document.getElementById('action').click();")
TheSociety
  • 1,936
  • 2
  • 8
  • 20
  • You posted Java code, my question is for JS, I don't know how to translate your code to run on my nodejs server – Cold_Class May 06 '19 at 14:48