相关疑难解决方法(0)

如何从下拉列表中选择一个选项

我可以点击选择器,但我的问题是如何从下拉列表中选择一个选项?

  await page.click('#telCountryInput > option:nth-child(4)')
Run Code Online (Sandbox Code Playgroud)

单击使用CSS选择器的选项不起作用.

例如,从下面的列表中选择国家/地区代码,

在此输入图像描述

puppeteer

34
推荐指数
5
解决办法
3万
查看次数

使用Puppeteer和无头Chrome获取DOM节点文本

我正在尝试使用无头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)

node.js google-chrome-headless puppeteer

10
推荐指数
1
解决办法
1万
查看次数