Selenium与NodeJS-从TinyMCE iframe返回时,驱动程序对象会丢失上下文

时间:2019-10-25 13:22:12

标签: javascript node.js selenium google-chrome selenium-chromedriver

首先,我登录一个单独的页面以访问带有表单的页面。

正确登录后并切换到具有表单的页面,我可以访问其所有元素,并且可以自由编辑它们。 表单元素之一是TinyMCE iframe

输入iframe并对其进行修改后,我不知道如何返回本机表单并单击提交...

这是我的代码( my.js ):

const loginUrl = 'https://www.example.com/login';
const formUrl = 'https://www.example.com/form';
const {Builder, By, until} = require('selenium-webdriver');
const driver_ch = new Builder().forBrowser('chrome').build();
driver_ch.manage().window().maximize();
driver_ch.get(loginUrl);

const loginLink = driver_ch.wait(until.elementLocated(By.css('#login-box')), 20000);

loginLink.click().then(function() {

  driver_ch.wait(until.elementLocated(By.css('#login'))).sendKeys('user_name');
  driver_ch.wait(until.elementLocated(By.css('#password'))).sendKeys('secret_password');
  driver_ch.wait(until.elementLocated(By.css('#submit-login'))).click();

}).then(function() {

  driver_ch.wait(until.elementLocated(By.css('.form-action'))).then(function() {

    driver_ch.get(formUrl);

    driver_ch.switchTo().frame(driver_ch.findElement(By.id("edit-body-und-0-value_ifr")));
    driver_ch.wait(until.elementLocated(By.id('tinymce'))).sendKeys('Glory! Glory! Hallelujah!');

直到现在一切正常,但这不起作用:

driver_ch.switchTo().defaultContent();

这也不是:

driver_ch.switchTo().parentFrame();

所以这无法完成...

    driver_ch.findElement(By.css('#edit-submit')).click();
  });
});

运行 my.js 脚本后出现错误信息:

node my.js 
(node:3955) UnhandledPromiseRejectionWarning: StaleElementReferenceError: stale element reference: element is not attached to the page document
  (Session info: chrome=74.0.3729.169)
  (Driver info: chromedriver=2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706),platform=Linux 4.9.0-9-amd64 x86_64)
    at Object.checkLegacyResponse (/home/user1/test/node_modules/selenium-webdriver/lib/error.js:585:15)
    at parseHttpResponse (/home/user1/test/node_modules/selenium-webdriver/lib/http.js:554:13)
    at Executor.execute (/home/user1/test/node_modules/selenium-webdriver/lib/http.js:489:26)
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:3955) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:3955) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

将驱动程序从 Chrome 更改为 FireFox 并不能解决问题。

已编辑:

感谢您 @DebanjanB 在回答中的建议。

我已经按照您的建议更新了Chrome浏览器和Chrome驱动程序,但是很遗憾,它仍然无法正常工作...

我现在收到此错误消息:

node my.js 
(node:24587) UnhandledPromiseRejectionWarning: StaleElementReferenceError: stale element reference: element is not attached to the page document
  (Session info: chrome=78.0.3904.70)
    at Object.throwDecodedError (/home/user1/test/node_modules/selenium-webdriver/lib/error.js:550:15)
    at parseHttpResponse (/home/user1/test/node_modules/selenium-webdriver/lib/http.js:563:13)
    at Executor.execute (/home/user1/test/node_modules/selenium-webdriver/lib/http.js:489:26)
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:24587) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promis
e which was not handled with .catch(). (rejection id: 1)
(node:24587) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zer
o exit code.

1 个答案:

答案 0 :(得分:1)

此错误消息...

node my.js 
(node:3955) UnhandledPromiseRejectionWarning: StaleElementReferenceError: stale element reference: element is not attached to the page document
  (Session info: chrome=74.0.3729.169)
  (Driver info: chromedriver=2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706),platform=Linux 4.9.0-9-amd64 x86_64)
    at Object.checkLegacyResponse (/home/user1/test/node_modules/selenium-webdriver/lib/error.js:585:15)
    at parseHttpResponse (/home/user1/test/node_modules/selenium-webdriver/lib/http.js:554:13)
    at Executor.execute (/home/user1/test/node_modules/selenium-webdriver/lib/http.js:489:26)
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:3955) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:3955) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

...表示 ChromeDriver 无法与所需元素进行交互,即 defaultContent / parentFrame

我在您的代码试用中没有发现任何重大问题。但是,您的主要问题似乎是所使用的二进制版本之间的不兼容性

  • 您正在使用 chromedriver = 2.41
  • chromedriver=2.41的发行说明中明确提到以下内容:
  

支持 Chrome v67-69

  

支持 Chrome v74

因此 ChromeDriver v2.41 Chrome浏览器v74.0

之间显然不匹配

解决方案

确保:

  • ChromeDriver 已更新为当前的ChromeDriver v78.0级别。
  • Chrome 已更新为当前的 Chrome版本78.0 级别。 (根据ChromeDriver v78.0 release notes
  • 通过您的 IDE
  • 清理您的项目工作区重建您的项目,并且仅具有必需的依赖项。
  • 如果您的基本 Web客户端版本太旧,则将其卸载并安装最新版本的 Web客户端 GA。
  • 进行系统重启
  • 执行您的@Test