การตั้งค่าพร็อกซี Selenium เชื่อมต่อ SOCKS5 และ HTTP Proxy กับ Chrome และ Firefox
คู่มือนี้จะอธิบายวิธีการเชื่อมต่อ HTTP Proxy และ SOCKS5 Proxy ของ Socks5Proxy เมื่อรัน Chrome และ Firefox ด้วย Selenium 4 และการตั้งค่า IP authorization เนื่องจากอาร์กิวเมนต์การรัน Chrome ไม่รับรหัสผ่าน Proxy
เหตุใดการรับรองพร็อกซีใน Selenium จึงเป็นเรื่องยาก?
Chrome accepts proxy addresses via the --proxy-server argument, but it does not accept the username and password within the address. When connecting to a proxy that requires a username and password, the browser will display an authentication window, which is difficult to fill in with Selenium.
วิธีที่สะอาดที่สุดคือการอนุญาต IP ลงทะเบียน IPv4 ของเซิร์ฟเวอร์หรือ PC ที่รัน Selenium ในแดชบอร์ด เพื่อให้พร็อกซียอมรับการเชื่อมต่อจาก IP นั้นโดยไม่ต้องใช้ชื่อผู้ใช้และรหัสผ่าน ดังนั้นใน Chrome เพียงใส่ ip:port ได้เลย ลงทะเบียนได้สูงสุด 10 IP เพื่อใช้ร่วมกันจากหลายเซิร์ฟเวอร์
ตัวอย่างใช้ IP 198.51.100.23 พอร์ต HTTP 8000 และพอร์ต SOCKS5 1080
pip install seleniumHow to connect HTTP proxy to Chrome?
หลังจากลงทะเบียนการรับรอง IP ให้เพิ่มที่อยู่ในอาร์กิวเมนต์ --proxy-server ของ ChromeOptions Selenium 4.6 ขึ้นไปจะใช้ Selenium Manager ดาวน์โหลด chromedriver ที่เหมาะสมโดยอัตโนมัติ ดังนั้นไม่จำเป็นต้องติดตั้งแยกต่างหาก
พารามิเตอร์เดียวกันนี้ใช้ได้ในโหมด headless เช่นกัน ในการตรวจสอบการเชื่อมต่อ ให้เปิดหน้าเว็บตรวจสอบ IP และดูว่า IP ที่แสดงเป็น IP ของพร็อกซี่หรือไม่
from selenium import webdriver
from selenium.webdriver.common.by import By
opts = webdriver.ChromeOptions()
opts.add_argument("--proxy-server=http://198.51.100.23:8000") # this machine's IP is authorized in the dashboard
opts.add_argument("--headless=new")
driver = webdriver.Chrome(options=opts)
try:
driver.get("https://api.ipify.org")
print(driver.find_element(By.TAG_NAME, "body").text) # should print the proxy IP
finally:
driver.quit()How to use SOCKS5 proxy in Chrome?
ใส่ที่อยู่ socks5:// ใน --proxy-server Chrome จะใช้ SOCKS5 สำหรับการ resolve ชื่อโดเมน แต่ในบางสภาพแวดล้อมอาจยังมีการ resolve DNS ภายใน คุณสามารถใช้ --host-resolver-rules เพื่อปิดการ resolve ชื่อภายในได้
SOCKS5 ยังรองรับการเชื่อมต่อด้วยการรับรอง IP เนื่องจาก Chrome ไม่รองรับการรับรองด้วยชื่อผู้ใช้และรหัสผ่านสำหรับพร็อกซี SOCKS
from selenium import webdriver
opts = webdriver.ChromeOptions()
opts.add_argument("--proxy-server=socks5://198.51.100.23:1080")
# make sure host names are resolved through the proxy
opts.add_argument('--host-resolver-rules=MAP * ~NOTFOUND , EXCLUDE 198.51.100.23')
driver = webdriver.Chrome(options=opts)
driver.get("https://api.ipify.org")
print(driver.page_source)
driver.quit()How to connect to Firefox?
Firefox specifies proxies via preference values. Set network.proxy.type to 1 (manual) and enter the HTTP or SOCKS address and port. For SOCKS5, set network.proxy.socks_version to 5 and network.proxy.socks_remote_dns to True for name resolution via the proxy.
Firefox also lacks a method to input proxy passwords via launch settings, making IP authorization the simplest option.
from selenium import webdriver
opts = webdriver.FirefoxOptions()
opts.set_preference("network.proxy.type", 1)
opts.set_preference("network.proxy.socks", "198.51.100.23")
opts.set_preference("network.proxy.socks_port", 1080)
opts.set_preference("network.proxy.socks_version", 5)
opts.set_preference("network.proxy.socks_remote_dns", True)
driver = webdriver.Firefox(options=opts)
driver.get("https://api.ipify.org")
print(driver.page_source)
driver.quit()วิธีการเปิดเบราว์เซอร์หลายตัวด้วย IP ที่ต่างกันพร้อมกัน?
เพียงเพิ่ม proxy address ที่ต่างกันสำหรับแต่ละเบราว์เซอร์และเรียกใช้งานแยกกัน ตัวอย่างโค้ดด้านล่างรับรายการ ip:port จาก API และเปิดเบราว์เซอร์ 3 ตัวด้วย IP ที่ต่างกัน เนื่องจากเบราว์เซอร์แต่ละตัวใช้หน่วยความจำมาก ควรกำหนดจำนวนที่เปิดพร้อมกันให้เหมาะสมกับขนาดเซิร์ฟเวอร์
การใช้งานที่ต้องคงเซสชันการเข้าสู่ระบบเป็นเวลานาน เช่น การใช้งานเบราว์เซอร์ ควรใช้ IP เดียวกับเบราว์เซอร์นั้น และไม่เปลี่ยน IP จนกว่าการทำงานจะเสร็จสิ้น
import requests
from selenium import webdriver
KEY = "YOUR_API_KEY"
lines = requests.get(
"https://socks5proxy.io/api/v1/proxies/download",
params={"format": "ip:port", "protocol": "http"},
headers={"Authorization": "Bearer " + KEY},
timeout=30,
).text.split()
drivers = []
for line in lines[:3]:
opts = webdriver.ChromeOptions()
opts.add_argument("--headless=new")
opts.add_argument("--proxy-server=http://" + line)
drivers.append(webdriver.Chrome(options=opts))
for d in drivers:
d.get("https://api.ipify.org")
print(d.page_source)
d.quit()หากจำเป็นต้องใช้การยืนยันตัวตนด้วยชื่อผู้ใช้และรหัสผ่านต้องทำอย่างไร
หากอยู่ในสภาพแวดล้อมที่ไม่สามารถใช้การรับรอง IP ได้ เช่น สภาพแวดล้อมที่ IP ออกเปลี่ยนไปเรื่อยๆ มีสองวิธี วิธีแรกคือใช้ไลบรารีพร็อกซีกลางเช่น selenium-wire เพื่อใส่ข้อมูลรับรองแทน และวิธีที่สองคือเปิดพร็อกซีกลางท้องถิ่นที่ไม่ต้องรับรองบนเซิร์ฟเวอร์เดียวกันเพื่อให้ Chrome เชื่อมต่อกับที่อยู่ท้องถิ่น และพร็อกซีกลางท้องถิ่นเชื่อมต่อกับ Socks5Proxy ด้วยชื่อผู้ใช้และรหัสผ่าน
ทั้งสองวิธีเพิ่มองค์ประกอบที่ต้องจัดการมากขึ้น หากเป็นไปได้ การใช้ IP authorization จากเซิร์ฟเวอร์ที่มี IP คงที่จะง่ายและเร็วที่สุด
ต้องตั้งค่าอะไรบ้างเพื่อให้สภาพแวดล้อมเบราว์เซอร์ตรงกับประเทศนั้น
เปลี่ยนอาร์กิวเมนต์ --lang และการตั้งค่า Accept-Language ของ Chrome ให้ตรงกับประเทศ IP เพื่อให้หน้าเว็บเปิดในภาษาของประเทศนั้น หากเว็บไซต์มีความสำคัญกับเขตเวลา สามารถตั้งค่าเขตเวลาให้ตรงกันได้ด้วยคำสั่ง CDP Emulation.setTimezoneOverride
กำหนดขนาดหน้าต่างด้วยอาร์กิวเมนต์ --window-size และใช้การตั้งค่าเลียนแบบอุปกรณ์มือถือหากต้องการหน้าจอมือถือ คุณสามารถสร้างชุดการตั้งค่าเหล่านี้สำหรับแต่ละประเทศเพื่อรันการทดสอบเดียวกันในหลายประเทศอย่างสม่ำเสมอ
from selenium import webdriver
opts = webdriver.ChromeOptions()
opts.add_argument("--proxy-server=http://198.51.100.23:8000")
opts.add_argument("--lang=de-DE")
opts.add_argument("--window-size=1366,900")
driver = webdriver.Chrome(options=opts)
driver.execute_cdp_cmd("Emulation.setTimezoneOverride", {"timezoneId": "Europe/Berlin"})
driver.get("https://example.com/")
driver.quit()Selenium Grid หรือเบราว์เซอร์ระยะไกลใช้การตั้งค่าเดียวกันหรือไม่?
ใช่ ChromeOptions และ FirefoxOptions ยังคงใช้งานได้ในการรันระยะไกล เพิ่มตัวเลือกเดียวกันใน webdriver.Remote เพื่อให้เบราว์เซอร์ใน Grid node เชื่อมต่อผ่าน proxy นั้น IP ที่จะลงทะเบียนใน IP authorization คือ IP ออกของ Grid node ที่เบราว์เซอร์รันจริง ไม่ใช่ PC ที่รันโค้ด
หากมีโหนดหลายเครื่อง ให้ลงทะเบียน IP ของทุกโหนด ลงทะเบียนได้สูงสุด 10 IP หากโหนดคลาวด์ของคุณเปลี่ยนแปลงบ่อย การกำหนด IP คงที่จะช่วยให้จัดการได้ง่ายขึ้น
from selenium import webdriver
opts = webdriver.ChromeOptions()
opts.add_argument("--proxy-server=http://198.51.100.23:8000") # authorize the Grid node IP, not this machine
driver = webdriver.Remote(command_executor="http://grid.example.internal:4444", options=opts)
driver.get("https://api.ipify.org")
print(driver.page_source)
driver.quit()ต้องตรวจสอบอะไรเมื่อเชื่อมต่อไม่สำเร็จ?
ERR_PROXY_CONNECTION_FAILED often occurs due to incorrect addresses or ports. Ensure you are not using a SOCKS5 port with an HTTP address. If ERR_TUNNEL_CONNECTION_FAILED appears or an authentication window pops up, the IP registered for IP authorization differs from the actual outgoing IP. Use cURL on the server to open api.ipify.org without a proxy and register the displayed IP.
เซิร์ฟเวอร์คลาวด์อาจเปลี่ยน IP ขาออกเมื่อรีสตาร์ท ใช้ IP คงที่ หรืออัปเดตรายการที่ลงทะเบียนในแดชบอร์ดเมื่อ IP เปลี่ยน