首页 > 解决方案 > Selenium 在无头模式下使用 nodejs 运行一次

问题描述

我正在尝试从 twitter 和 mac 上的不和谐中抓取一些文本。这是我使用的代码:

const url_twitter = "https://twitter.com/CryptoKitties"
const url_discord = "https://discord.com/invite/cryptokitties"

const twitter_follower_xpath = '//*[@id="react-root"]/div/div/div[2]/main/div/div/div/div/div/div[2]/div/div/div[1]/div/div[5]/div[2]/a/span[1]/span' 
const discord_members_xpath = '//*[@id="app-mount"]/div[2]/div/div[2]/div/div/div/form/div/div[1]/div[2]/div[2]/span'

const webdriver = require ('selenium-webdriver')
const chrome = require('selenium-webdriver/chrome');
const By = webdriver.By
const until = webdriver.until;
var path = require('chromedriver').path;

var service = new chrome.ServiceBuilder(path).build();
chrome.setDefaultService(service);

const driver = new webdriver.Builder()
.forBrowser('chrome')
.setChromeOptions(new chrome.Options().headless())
.build();


async function getFollowers(url, followers_xpath, timeout){

// getting the url
await driver.get(url)

// waiting 
//await new Promise(resolve => setTimeout(resolve, timeout));
const text = await driver.wait(await until.elementLocated(await By.xpath(followers_xpath), timeout)).getText()
console.log(text)

  }

async function main() {
    await getFollowers(url_twitter, twitter_follower_xpath, 6000)
    await getFollowers(url_discord, discord_members_xpath, 6000)
    }

//getFollowers(url_twitter, twitter_follower_xpath, 6000)
getFollowers(url_discord, discord_members_xpath, 6000)

问题是代码只运行一次,如果我取消注释第一个抓取 twitter 的语句,它就会挂起

标签: node.jsseleniumwebdriver

解决方案


推荐阅读