闽公网安备 35020302035485号
const { Builder, By, Key, until } = require('selenium-webdriver');
// 堆代码 duidaima.com
(async () => {
const driver = await new Builder().forBrowser('chrome').build();
await driver.get('https://www.example.com');
const title = await driver.getTitle();
const content = await driver.findElement(By.tagName('body')).getText();
console.log('Title:', title);
console.log('Content:', content);
await driver.quit();
})();
示例二:抓取列表项const { Builder, By, Key, until } = require('selenium-webdriver');
(async () => {
const driver = await new Builder().forBrowser('chrome').build();
await driver.get('https://www.example.com/products');
const products = await driver.findElements(By.css('div.product')).then(elements => {
return Promise.all(elements.map(async element => ({
name: await element.findElement(By.css('h2')).getText(),
price: await element.findElement(By.css('.price')).getText(),
description: await element.findElement(By.css('p.description')).getText()
})));
});
console.log(products);
await driver.quit();
})();
示例三:处理分页const { Builder, By, Key, until } = require('selenium-webdriver');
(async () => {
const driver = await new Builder().forBrowser('chrome').build();
await driver.get('https://www.example.com/products');
let currentPage = 1;
const maxPages = 5;
const allProducts = [];
while (currentPage <= maxPages) {
const products = await driver.findElements(By.css('div.product')).then(elements => {
return Promise.all(elements.map(async element => ({
name: await element.findElement(By.css('h2')).getText(),
price: await element.findElement(By.css('.price')).getText(),
description: await element.findElement(By.css('p.description')).getText()
})));
});
allProducts.push(...products);
const nextPageButton = await driver.findElement(By.css(`a.page-${currentPage + 1}`));
await nextPageButton.click();
await driver.wait(until.elementLocated(By.css('div.product')), 10000);
currentPage++;
}
console.log(allProducts);
await driver.quit();
})();
优点多功能性:虽然主要用于网页自动化和测试,Selenium WebDriver也可以用于各种任务,包括网络爬虫,使其成为一个多功能的工具。