使用 NodeJS 和 Puppeteer 单击随机的 Google 搜索结果? [英] Click on random Google Search result using NodeJS and Puppeteer?

查看:40
本文介绍了使用 NodeJS 和 Puppeteer 单击随机的 Google 搜索结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个小脚本,以便在搜索'what is ' + Word"后点击随机的 Google 搜索结果.我所做的一切都无法获得我想要的结果,哎呀,我什至无法获得点击单个 Google 搜索结果的脚本!

I'm attempting on making a small script to click on a random Google Search result after searching "'what is ' + Word." Nothing I've done has been able to get me the results I want, heck, I can't even get the script to click a single Google Search result!

我在这里尝试了多种操作,例如将所有搜索结果收集到一个数组中,然后单击随机一个(没有收集到一个数组中),按部分文本单击一个元素(https://没有结果)),以及许多其他适用于 Python 的解决方案,但在此处不起作用.

I've tried doing multiple things here, such as collecting all search results in an array and clicking a random one (didn't collect into an array), clicking an element by partial text (https:// brought no results), and many other solutions that work in Python, but don't work here.

const puppeteer = require('puppeteer');
const searchbar = "#tsf > div:nth-child(2) > div > div.RNNXgb > div >   div.a4bIc > input"



async function gsearch() {
const browser = await puppeteer.launch({headless:false, args:['--no-sandbox', '--disable-setuid-sandbox']});
const page = await browser.newPage();

await page.goto('https://google.com');
 var fs  = require("fs");
var array = fs.readFileSync("words.txt").toString().split('\n');
var random = array[Math.floor(Math.random() * array.length)]
await page.click(searchbar)
await page.keyboard.type("what is " + random);
await page.waitFor(1000);
await page.evaluate(() => {
  let elements = $('LC20lb').toArray();
  for (i = 0; i < elements.length; i++) {
    $(elements[i]).click();


  } 
 })
}

gsearch();

(忽略任何缩进继承错误,我发誓它在 VSC 中看起来更干净)

(ignore any indent-inheritant errors, I swear it looks cleaner in VSC)

预期点击随机搜索结果.最终什么也没做,也许是一两个错误,但仅此而已.

Expected to click a random search result. End up getting nothing done, maybe an error or two but that's about it.

推荐答案

LC20lb 不是 html 标签,它应该是 h3 的类名并使用$() 你想用 jQuery 选择元素吗?改用 document.querySelectorAll().

LC20lb is not html tag and it should be class name for h3 and by using$() are you trying to select elements with jQuery? use document.querySelectorAll() instead.

const puppeteer = require('puppeteer');
const fs = require("fs");

async function gsearch() {
  const browser = await puppeteer.launch({
    headless: false,
    args: ['--no-sandbox', '--disable-setuid-sandbox']
  });
  const page = await browser.newPage();

  await page.goto('https://google.com');
  var array = fs.readFileSync("words.txt").toString().split('\n');
  var random = array[Math.floor(Math.random() * array.length)];
  // simple selector for search box
  await page.click('[name=q]');
  await page.keyboard.type("what is " + random);
  // you forgot this
  await page.keyboard.press('Enter');
  // wait for search results
  await page.waitForSelector('h3.LC20lb', {timeout: 10000});
  await page.evaluate(() => {
    let elements = document.querySelectorAll('h3.LC20lb')
    // "for loop" will click all element not random
    let randomIndex = Math.floor(Math.random() * elements.length) + 1
    elements[randomIndex].click();
  })
}

这篇关于使用 NodeJS 和 Puppeteer 单击随机的 Google 搜索结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆