أتمتة مهام المتصفح باستخدام Selenium و Proxy

أدوات الأتمتة مثل Selenium قوية جداً لاختبار المواقع أو جمع البيانات. لكن استخدامها من عنوان IP واحد وبشكل متكرر يؤدي للحظر السريع. الحل هو دمج Residential Proxies في كود الأتمتة لتوزيع الطلبات والظهور كمستخدمين متعددين.

إعداد البروكسي في Chrome WebDriver

لا يدعم Selenium تغيير البروكسي أثناء التشغيل (Runtime) بسهولة، لذلك يجب ضبطه عند بدء الجلسة. إليك كيفية القيام بذلك باستخدام Python.

كود Python مع Selenium


from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time

PROXY = "123.45.67.89:8080"  # ضع عنوان البروكسي هنا

chrome_options = Options()
chrome_options.add_argument(f'--proxy-server={PROXY}')

# خيارات إضافية لتجنب الكشف
chrome_options.add_argument("user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)...")

driver = webdriver.Chrome(options=chrome_options)

try:
    driver.get("https://whatismyipaddress.com")
    time.sleep(5)
    print("Page Title:", driver.title)
finally:
    driver.quit()
                    

التعامل مع البروكسي الذي يتطلب مصادقة (Auth)

متصفح Chrome لا يدعم تمرير اسم المستخدم وكلمة المرور للبروكسي عبر سطر الأوامر مباشرة. لحل هذه المشكلة، نستخدم إضافة (Extension) صغيرة يتم إنشاؤها ديناميكياً لحقن بيانات الاعتماد، أو نستخدم أدوات مثل selenium-wire التي تسهل هذه العملية بشكل كبير.

نصائح لتجنب الحظر

  • قم بتدوير الـ User-Agent مع كل جلسة جديدة.
  • استخدم تأخيرات عشوائية (Random Delays) بين الإجراءات.
  • لا تعتمد على البروكسيات المجانية؛ فهي غالباً ما تكون محظورة بالفعل (Blacklisted).

Disclosure: we may earn a commission if you purchase through some links.

Services Related To This Article

These offers are related to privacy, password security, and development workflows. Replace the current links with your real affiliate links through environment variables.

Default Links Need Replacement

Proton

Privacy, mail, and VPN

A strong fit for privacy-focused visitors who want VPN and secure email under one brand.

Explore Proton

Surfshark

Consumer VPN

Best on pages about geo-blocking, privacy, and public Wi-Fi protection.

Try Surfshark

NordVPN

High-converting VPN

A clear offer for users who need a paid, more durable alternative to a free proxy.

View NordVPN

1Password

Password management

The best match for password, secrets, team access, and personal security pages.

Discover 1Password

DigitalOcean

Developer cloud hosting

A strong fit for proxy setup, server, and infrastructure content aimed at developers and small teams.

Start with DigitalOcean