Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
78 views
in Technique[技术] by (71.8m points)

javascript - page.type() inside page.evaluate()

I want to type some user details in following input class

<span class="myClass">
    <input aria-autocomplete="list" role="searchbox" aria-haspopup="true" class="ng-tns-c58-69" 
    autocomplete="off" type="text" aria-controls="pr_id_7_list" aria-expanded="false" aria- 
    activedescendant="p-highlighted-option" placeholder="Pass" size="24" maxlength="16">
</span>

First, I tried use page.$eval() method to input details

let domain = 'input[class=ng-tns-c58-69]';
await page.$eval(domain, (el, value) => el.value = value, pass.name);

but it threw error:

Error: Evaluation failed: DOMException: Failed to execute 'querySelector' on 'Document': 
'input[class=ng-tns-c58-69]' is not a valid selector.

Then I used page.evaluate() method as

await page.evaluate(() => {
    let domain = 'ng-tns-c58-69';
    let elements = document.getElementsByClassName(domain);
    for (let el of elements) {
        el.click();
        el.keyboard.type(passenger[0].name, {delay: 100});
    }
})

that gave error saying:

Error: Evaluation failed: TypeError: Cannot read property 'type' of undefined

I am new to puppeteer, Kindly help me. Thank you.

question from:https://stackoverflow.com/questions/65949493/page-type-inside-page-evaluate

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As suggested by pavelsaman, used input.ng-tns-c58-69 with page.$eval() method and it worked. Here is the final code:

let domain = 'input.ng-tns-c58-69';
await page.$eval(domain, (el, value) => el.value = value, pass.name);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...