首先,我在一个单独的页面上登录,以访问带有表单的页面。
正确登录并切换到带有表单的页面后,我可以访问它的所有元素,并且可以自由编辑它们表单元素之一是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的发行说明明确提到以下内容:
支持Chrome v67-69
- 您使用的是chrome=chrome=74.0
- ChromeDriver v74.0.3729.6的发行说明明确提到以下内容:
支持Chrome v74
因此ChromeDriverv2.41和Chrome浏览器v74.0之间存在明显的不匹配
解决方案
确保:
- ChromeDriver更新到当前ChromeDriverv78.0级别
- Chrome更新到当前Chrome 78.0版本。(根据ChromeDriver v78.0发布说明(
- 通过IDE清理您的项目工作区,并仅使用所需的依赖项重建项目
- 如果您的基本Web客户端版本太旧,请将其卸载并安装最新的GA和发布的Web客户端版本
- 进行系统重新启动
- 执行您的
@Test