0

I have the following Selenium script, which works correctly in Chrome and Firefox:

  await driver.get("http://localhost:44180/");
  await driver.wait(until.titleIs(RP_LOGIN_TITLE), TIMEOUT);

  const emailInput = await driver.findElement(By.name("email"));
  await emailInput.sendKeys(JOHN_EMAIL, Key.RETURN);
  await driver.wait(until.titleIs(BROKER_CONFIRM_TITLE), TIMEOUT);

  const mail = mailbox.nextMail();
  const match = /^[a-z0-9]{6} [a-z0-9]{6}$/m.exec(mail);
  if (!match) {
    throw Error("Could not find the verification code in the email text");
  }
  const code = match[0];

  // await driver.wait(until.elementLocated(By.name("code")), TIMEOUT);
  const codeInput = await driver.findElement(By.name("code"));
  await codeInput.sendKeys(code, Key.RETURN);
  await driver.wait(until.titleIs(RP_CONFIRMED_TITLE), TIMEOUT);

The HTML of the first loaded page looks like:

<html>
  <head>
    <title>Login</title>
  </head>
  <body>
    <form action="/auth" method="post">
      <input name="email" type="email" />
    </form>
  </body>
</html>

The line that sends the Enter key triggers a form submission, which causes a redirect to a page with the following HTML:

<html>
  <head>
    <title>Confirm</title>
  </head>
  <body>
    <form id="form" action="/confirm" method="post">
      <input
        name="session"
        type="hidden"
        value="NGd7Y-OpQYrZ6wWwZWRKWHyukIVRiu5Pby0bCVshIlQ"
      />
      <div class="entry">
        <input
          name="code"
          autofocus=""
          type="text"
          maxlength="20"
          autocapitalize="off"
          autocorrect="off"
          autocomplete="off"
        /><button type="submit">Login</button>
      </div>
    </form>
  </body>
</html>

The call to await driver.findElement(By.name("code")) throws a NoSuchElementError. What's interesting, though, is if I add console.log(await driver.getPageSource()) right before that line, the returned HTML has the element. In fact, if I update that line to await driver.findElement(By.css("body")) I still get a NoSuchElementError.

I'm using version v4.0.0-alpha.7 of the selenium-webdriver npm package, version 3.150.1 of the 32-bit IEDriverServer, and IE11 build 11.329.19041.0 on Windows 10 2004.

Does anyone have any ideas why, after form submission and navigation, calls to findElement throw NoSuchElementError despite calls like getPageSource clearly seeing the element?

dstaley
  • 1,002
  • 1
  • 12
  • 32
  • Does the element in an iframe? In that case you need to switch to the iframe where it locates. Another situation is that your Selenium scripts run faster than the element loads, the element is not present (yet) in the HTML document. You could wait for the element to appear by adding `wait()` function before it. – Yu Zhou Jul 08 '20 at 02:47
  • No, the element isn't in an iframe. In the code above you can see I commented out a line that waits for the element. If I uncomment it, the script times out waiting for the element. – dstaley Jul 08 '20 at 05:16
  • Does the `sendKeys` cause a redirect? It could be better if you provide the link of the page you're automating. It's hard to debug and locate the issue without the actual structure of the webpage. Now we can only give some assumptions. You could refer to the accepted answer in [this thread](https://stackoverflow.com/questions/48471321/nosuchelementexception-selenium-unable-to-locate-element). It shows many scenario of `NoSuchElementError`, you could check them one by one. – Yu Zhou Jul 09 '20 at 07:00
  • I've updated the post to include more details. Yes, `sendKeys` causes a redirect. I think the most important fact is this Selenium script works fine in Chrome and Firefox, so it's not a simple issue like the ones described in your linked question. – dstaley Jul 09 '20 at 21:40

1 Answers1

0

Which versions of InternetExplorerDriver and Selenium server are you using?

I use InternetExplorerDriver 3.14.0. I think the issue won't be related with language so I test using Java language binding.

First, I test with selenium server 4.0.0-alpha.5 and it shows error in the same place as your code. It shows Error communicating with the remote browser which looks like the connection being lost. Then I test with selenium server 3.141.59 and it works.

If you're using the alpha version of selenium, I suggest you use the lower version of selenium such as the stable version in this link.

Yu Zhou
  • 11,532
  • 1
  • 8
  • 22
  • I've updated the post with version details. I'm using selenium-webdriver `v4.0.0-alpha.7`, but am unable to run `v3.6.0` due to https://github.com/SeleniumHQ/selenium/issues/7986. – dstaley Jul 10 '20 at 16:25
  • I think that should be an issue with the Selenium WebDriver as it has problem when automating IE in alpha version. I suggest you to [open an issue](https://github.com/SeleniumHQ/selenium/issues) in github to see if the Selenium team would fix it. – Yu Zhou Jul 13 '20 at 02:13