node.js - 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 的语句,它就会挂起
解决方案
推荐阅读
- amazon-web-services - 将 PHP Codeigniter 项目转移/迁移到 AWS
- django - pgAdmin4 客户端运行时,Django migrate/makemigrations 不起作用
- javascript - 函数声明后如何分配 JavaScript 变量?
- java - 自动化的 IndexOutOfBoundsException
- java - 首次操作的休眠时间 - 太慢了
- python - 无限加载从新闻网站上抓取标题
- android - 使用脚本将生成的 .apk 上传到 Diawi
- sql - 如何从表中选择带有 oracle sql 中的 group by 子句的嵌套 json 对象?
- javascript - TypeError:未定义不是对象(评估“navigation.state”)
- xamarin - 如何在 C# 中为 ContentPage 设置 Shell.NavBarIsVisible="false"?