Skip to content

Instantly share code, notes, and snippets.

@SarahElson
Last active September 15, 2022 06:04
How To Perform Web Scraping With JavaScript And Selenium
const express = require('express');
const { Builder, By } = require('selenium-webdriver');
const app = express();
const port = 3000;
app.get('/', async (request, response) => {
// Web Scraping Code here
try {
const data = await WebScrapingLocalTest();
response.status(200).json(data);
} catch (error) {
response.status(500).json({
message: 'Server error occurred',
});
}
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});
async function WebScrapingLocalTest() {
try {
driver = await new Builder().forBrowser('chrome').build();
await driver.get('https://www.youtube.com/c/LambdaTest/videos');
const allVideos = await driver.findElements(
By.css('ytd-grid-video-renderer.style-scope.ytd-grid-renderer')
);
return await getVideos(allVideos);
} catch (error) {
throw new Error(error);
} finally {
await driver.quit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment