首先,我登录一个单独的页面以访问包含表单的页面。
正确登录并切换到表单页面后,我可以访问其所有元素,并且可以自由编辑它们。 表单元素之一是 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.
最佳答案
此错误消息...
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 的发行说明明确提到以下内容:
Supports Chrome v67-69
- 您正在使用 chrome= chrome=74.0
- ChromeDriver v74.0.3729.6 的发行说明明确提到以下内容:
Supports Chrome v74
因此,ChromeDriver v2.41 和 Chrome 浏览器 v74.0 之间存在明显的不匹配
<小时/>解决方案
确保:
- ChromeDriver 已更新为当前 ChromeDriver v78.0级别。
- Chrome 已更新至当前 Chrome 版本 78.0 级别。 (根据 ChromeDriver v78.0 release notes )
- 通过 IDE清理您的项目工作区,并仅使用所需的依赖项重建项目。
- 如果您的基本Web 客户端版本太旧,请将其卸载并安装最新的 GA 和已发布版本的Web 客户端。
- 重新启动系统。
- 执行您的
@Test
。
关于javascript - Selenium 与 NodeJS - 从 TinyMCE iframe 返回时驱动程序对象丢失上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58559389/