我可以点击选择器,但我的问题是如何从下拉列表中选择一个选项?
await page.click('#telCountryInput > option:nth-child(4)')
Run Code Online (Sandbox Code Playgroud)
单击使用CSS选择器的选项不起作用.
例如,从下面的列表中选择国家/地区代码,
我正在尝试使用无头Chrome和Puppeteer来运行我们的Javascript测试,但我无法从页面中提取结果.基于这个答案,看起来我应该使用page.evaluate().该部分甚至有一个看起来像我需要的例子.
const bodyHandle = await page.$('body');
const html = await page.evaluate(body => body.innerHTML, bodyHandle);
await bodyHandle.dispose();
Run Code Online (Sandbox Code Playgroud)
作为一个完整的示例,我尝试将其转换为一个脚本,该脚本将从Stack Overflow上的用户配置文件中提取我的名字.我们的项目使用的是Node 6,因此我将await表达式转换为使用.then().
const puppeteer = require('puppeteer');
puppeteer.launch().then(function(browser) {
browser.newPage().then(function(page) {
page.goto('https://stackoverflow.com/users/4794').then(function() {
page.$('h2.user-card-name').then(function(heading_handle) {
page.evaluate(function(heading) {
return heading.innerText;
}, heading_handle).then(function(result) {
console.info(result);
browser.close();
}, function(error) {
console.error(error);
browser.close();
});
});
});
});
});
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我收到此错误:
$ node get_user.js
TypeError: Converting circular structure to JSON
at Object.stringify (native)
at args.map.x (/mnt/data/don/git/Kive/node_modules/puppeteer/node6/helper.js:30:43)
at Array.map (native)
at Function.evaluationString (/mnt/data/don/git/Kive/node_modules/puppeteer/node6/helper.js:30:29) …Run Code Online (Sandbox Code Playgroud)