Skip to content

Instantly share code, notes, and snippets.

@semihkeskindev
Last active September 28, 2024 21:11
Show Gist options
  • Save semihkeskindev/53ed202af649870749a8f0661573039a to your computer and use it in GitHub Desktop.
Save semihkeskindev/53ed202af649870749a8f0661573039a to your computer and use it in GitHub Desktop.
Selenium Webdriver, listen network traffic
import {Builder, By, until} from 'selenium-webdriver';
import fs from 'fs';
// Tarayıcıyı başlat (bu örnekte Chrome kullanılıyor)
/** @type {WebDriver} */
let driver = await new Builder()
.forBrowser('chrome')
.setLoggingPrefs({performance: 'ALL'})
.build();
let captchaBase64Image = null;
// CDP oturumu başlat
/** @type {CDPConnection} */
const cdpConnection = await driver.createCDPConnection('page');
// Ağ olaylarını dinlemeye başla
await cdpConnection.execute('Network.enable');
// ResponseReceived olayını dinlemek için bir fonksiyon oluştur
const handleNetworkEvent = async (message) => {
const data = JSON.parse(message);
if (data.method === 'Network.responseReceived') {
const params = data.params;
// url içerisinde "captcha" kelimesi geçiyorsa aradığım network request'i bulmuşum demektir.
if (params.response.url.search('captcha') > 0) {
console.log('Captcha bulundu:', params.response.url);
try {
// Görsel içeriğini al
const result = await cdpConnection.send('Network.getResponseBody', {
requestId: params.requestId
});
// Base64 kodlu görsel içeriğini kaydet veya işle
const imageContent = result.result.body;
console.log('Görsel içeriği alındı, uzunluk:', imageContent.length);
await saveCaptchaImage(imageContent);
captchaBase64Image = imageContent;
} catch (error) {
console.error('Görsel içeriği alınırken hata oluştu:', error);
}
}
}
};
async function saveCaptchaImage(base64Image) {
// Decode the base64 image
const decodedImage = Buffer.from(base64Image, 'base64');
// Görselin uzantısı ne ise ona göre kaydedebilirsiniz. Örneğin .png, .jpg, .gif
fs.writeFileSync('./temp/captcha.png', decodedImage)
}
import {Builder, By, until} from 'selenium-webdriver';
import fs from 'fs';
async function saveCaptchaImage(base64Image) {
// Decode the base64 image
const decodedImage = Buffer.from(base64Image, 'base64');
// Görselin uzantısı ne ise ona göre kaydedebilirsiniz. Örneğin .png, .jpg, .gif
fs.writeFileSync('./temp/captcha.png', decodedImage)
}
async function runAutomation() {
// Tarayıcıyı başlat (bu örnekte Chrome kullanılıyor)
/** @type {WebDriver} */
let driver = await new Builder()
.forBrowser('chrome')
.setLoggingPrefs({performance: 'ALL'})
.build();
let captchaBase64Gif = null;
// CDP oturumu başlat
/** @type {CDPConnection} */
const cdpConnection = await driver.createCDPConnection('page');
// Ağ olaylarını dinlemeye başla
await cdpConnection.execute('Network.enable');
// Network'ten gelen mesajları işlemek için bir fonksiyon oluştur
const handleNetworkEvent = async (message) => {
const data = JSON.parse(message);
if (data.method === 'Network.responseReceived') {
const params = data.params;
// url içerisinde "captcha" kelimesi geçiyorsa aradığım network request'i bulmuşum demektir.
if (params.response.url.search('captcha') > 0) {
console.log('Captcha bulundu:', params.response.url);
try {
// Görsel içeriğini al
const result = await cdpConnection.send('Network.getResponseBody', {
requestId: params.requestId
});
// Base64 kodlu görsel içeriğini kaydet veya işle
const imageContent = result.result.body;
console.log('Görsel içeriği alındı, uzunluk:', imageContent.length);
await saveCaptchaImage(imageContent);
captchaBase64Gif = imageContent;
} catch (error) {
console.error('Görsel içeriği alınırken hata oluştu:', error);
}
}
}
};
// WebSocket bağlantısına event listener ekle
cdpConnection._wsConnection.on('message', handleNetworkEvent);
try {
// Siteye git
await driver.get('https://example.com');
// görselini almak istediğimiz elementin yüklenmesini bekle
await driver.wait(until.elementLocated(By.css('input[type="image"]')), 5000);
// Sayfa içeriğine göre sayfanın kendine gelmesini beklemesini isterseniz (opsiyonel)
await driver.sleep(1000);
if (!captchaBase64Gif) {
console.error('Captcha görseli alınamadı. Çıkış yapıyorum.');
process.exit(1);
}
} finally {
// Tarayıcıyı kapat
await driver.quit();
}
}
await runAutomation();
{
"name": "example",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"type": "module",
"dependencies": {
"selenium-webdriver": "^4.24.0"
}
}
@semihkeskindev
Copy link
Author

Requirement: NodeJS 20.17.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment