javascript - 如何使用 Puppeteer 单击 Cloud Saas 应用程序上的链接?
问题描述
我正在尝试使用 puppeteer 在 Oracle Cloud Saas ERP 应用程序上自动化一些数据输入和测试。
以下是步骤和代码。我遇到的问题是,每当我执行 page.click 元素时,我都会收到错误消息:
(node:58569) UnhandledPromiseRejectionWarning: Error: Evaluation failed: DOMException: Failed to execute 'querySelector' on 'Document': 'a#pt1:commandLink1.xj8.xit' is not a valid selector.
at <anonymous>:1:33
at ExecutionContext.evaluateHandle (/Users/user/node_modules/puppeteer/lib/ExecutionContext.js:88:13)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:160:7)
(node:58569) 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:58569) [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.
打开登录页面并登录到应用程序(作品)
const browser = await puppeteer.launch({headless: false});
let page = await browser.newPage();
await page.goto(config.appurl, {waitUntil: 'networkidle2'});
await page.type('#userid', 'USERNAME', {delay : 10});
await page.type('#password', 'PASSWORD', {delay : 10});
await page.waitForSelector('#btnActive', {enabled : true});
page.click('#btnActive', {delay : 1000}).then(() => console.log('Login Button Clicked'));
目标更改的日志 url (Works)
browser.on('targetchanged', ()=> {
console.log('Target Changed!');
console.log('URL: ', page.url());
});
渲染,屏幕截图并单击主页加载时的元素(渲染和屏幕截图按预期工作。我看到屏幕截图和渲染的 html 是用户登录后的主页,但点击会产生错误 - 不是有效的选择器)
page.on('load',()=> {
console.log('Home Page Loaded!');
console.log(page.url());
page.screenshot({path: 'home.png'});
async function render(page){
let html = await page.content();
await fs.writeFileSync('home.html', html);
}
render(page);
page.click('a#pt1:commandLink1.xj8.xit', {delay : 1000}).then(() => console.log('Link Clicked')); //this is what generates the error
});
从主页的呈现内容中,我看到下面的 html 片段,这是我尝试使用 page.click 进行单击的内容。
<div><a id="pt1:commandLink1" class="xj8 xit" onclick="return false;" href="#">You have a new home page!</a></div>
我看到使用 puppeteer 和 chrome 在 Cloud Saas App 上编写脚本的另一个问题是我无法在页面上的所有元素上使用 Inspect Element,不知道为什么。
如何更改我的脚本以便能够单击此页面上提到的链接?
解决方案
推荐阅读
- python - 每次创建新项目时都必须在 PyCharm 中安装 numpy 吗?(安装而不是导入)
- flutter - 在颤振中使用相同的 shared_preferences 实例是安全的
- python - 如何获取 python-chess 模块中所有合法移动的列表?
- php - 验证表单 Ajax PHP
- azure - 更改 Microsoft Azure 门户登录 ID
- javascript - PHP / Axios - 正确格式化电子邮件对象以传递给 PHP 邮件程序
- javascript - 在 ckeditor5 中提交时图像 src 丢失
- docker - 如何访问 docker 容器 localhost
- encryption - 哪种密码哈希算法与平台和语言无关?
- python - 为什么我收到请求的 405 错误?