2012-01-16 43 views
14

Tôi muốn sử dụng selen với proxy được bảo vệ bằng mật khẩu. Proxy không cố định, nhưng là một biến. Vì vậy, điều này đã được thực hiện trong mã (chỉ cần thiết lập firefox trên máy tính cụ thể này để làm việc với proxy là ít hơn lý tưởng). Cho đến nay tôi có đoạn mã sau:Selenium sử dụng Python: nhập/cung cấp mật khẩu proxy http cho firefox

fp = webdriver.FirefoxProfile() 
# Direct = 0, Manual = 1, PAC = 2, AUTODETECT = 4, SYSTEM = 5 
fp.set_preference("network.proxy.type", 1) 

fp.set_preference("network.proxy.http", PROXY_HOST) 
fp.set_preference("network.proxy.http_port", PROXY_PORT) 

driver = webdriver.Firefox(firefox_profile=fp) 
driver.get("http://whatismyip.com") 

Tại thời điểm này, hộp thoại bật lên yêu cầu người dùng/thẻ proxy.

Có một cách dễ dàng để một trong hai:

  1. Gõ vào user/pass trong hộp thoại.
  2. Cung cấp cho người dùng/đường chuyền ở giai đoạn sớm hơn.
+2

hey mate, bạn đã tìm thấy giải pháp chưa? – Shane

+1

@Shane bạn đã tìm thấy giải pháp chưa? –

+2

@ArsenIbragimov bạn đã tìm thấy giải pháp chưa? – Math

Trả lời

-4

Bạn đã thử PROXY_HOST = "http://username:[email protected]"?

Also:

Starting with Selenium 2.0 beta 1, there is built in support for handling popup dialog boxes.

+2

'PROXY_HOST =" tên người dùng: [email protected] "' không hoạt động. Khi xử lý hộp thoại bật lên, làm cách nào để tôi định vị các phần tử người dùng/vượt qua để nhập? – Shane

+0

@Shane, bạn đã thử 'PROXY_HOST =" tên người dùng: [email protected] "' hoặc 'PROXY_HOST =" http: // tên người dùng: [email protected] "'. Thứ hai nên vượt qua auth cơ bản. Về vị trí các phần tử: nếu bạn có thể chuyển sang hộp thoại, bạn luôn có thể điều hướng các phần tử của nó gửi tín hiệu khóa 'tab'. –

8

Selenium không thể làm điều đó bằng cách riêng của mình. Cách duy nhất tôi thấy hữu ích được mô tả here. Để ngắn gọn, bạn cần phải thêm tiện ích mở rộng trình duyệt khi đang thực hiện xác thực. Nó đơn giản hơn nhiều so với vẻ bề ngoài.

Sau đây là cách nó hoạt động dành cho Chrome (trong trường hợp của tôi):

  1. Tạo một file zip proxy.zip chứa hai tập tin:

background.js

var config = { 
    mode: "fixed_servers", 
    rules: { 
     singleProxy: { 
     scheme: "http", 
     host: "YOU_PROXY_ADDRESS", 
     port: parseInt(YOUR_PROXY_PORT) 
     }, 
     bypassList: ["foobar.com"] 
    } 
    }; 

chrome.proxy.settings.set({value: config, scope: "regular"}, function() {}); 

function callbackFn(details) { 
    return { 
     authCredentials: { 
      username: "YOUR_PROXY_USERNAME", 
      password: "YOUR_PROXY_PASSWORD" 
     } 
    }; 
} 

chrome.webRequest.onAuthRequired.addListener(
     callbackFn, 
     {urls: ["<all_urls>"]}, 
     ['blocking'] 
); 

Đừng quên thay thế YOUR_PROXY_ * vào cài đặt của bạn.

manifest.json

{ 
    "version": "1.0.0", 
    "manifest_version": 2, 
    "name": "Chrome Proxy", 
    "permissions": [ 
     "proxy", 
     "tabs", 
     "unlimitedStorage", 
     "storage", 
     "<all_urls>", 
     "webRequest", 
     "webRequestBlocking" 
    ], 
    "background": { 
     "scripts": ["background.js"] 
    }, 
    "minimum_chrome_version":"22.0.0" 
} 
  1. Thêm sự tạo proxy.zip như một phần mở rộng

    from selenium import webdriver 
    from selenium.webdriver.chrome.options import Options 
    
    chrome_options = Options() 
    chrome_options.add_extension("proxy.zip") 
    
    driver = webdriver.Chrome(executable_path='chromedriver.exe', chrome_options=chrome_options) 
    driver.get("http://google.com") 
    driver.close() 
    

Vậy là xong. Đối với tôi làm việc như một say mê. Nếu bạn cần tạo proxy.zip động hoặc cần ví dụ về PHP, hãy truy cập vào original post

+0

Tuyệt vời !!!!! Làm việc một say mê! Chúc mừng Mike –

+0

Mike, nó không hoạt động với tôi - http://joxi.net/LmG3OVNTRbEMam Dòng cuối cùng nói 'WebDriverException: Message: unknown error: không thể xử lý phần mở rộng # 1 từ lỗi không xác định: không thể đọc manifest (Thông tin trình điều khiển : chromedriver = 2.29.461591 (62ebf098771772160f391d75e589dc567915b233), nền tảng = Windows NT 10.0.14393 x86_64) ' –

1

Sau khi được lấy cảm hứng từ các kiểm tra đơn vị trong selenium github repo. Điều này làm việc cho tôi:

from selenium import webdriver 

PROXY_HOST = "IP_address" 
PROXY_PORT = 8080 
USERNAME = "your_user_name" 
PASSWORD = "YOUR_PASSWORD" 

profile = webdriver.FirefoxProfile() 
profile.set_preference("network.proxy.type", 1) 
profile.set_preference("network.proxy.http", PROXY_HOST) 
profile.set_preference("network.proxy.http_port", PROXY_PORT) 
profile.set_preference("network.proxy.socks_username", USERNAME) 
profile.set_preference("network.proxy.socks_password", PASSWORD) 

profile.update_preferences() 

# executable_path = define the path if u don't already have in the PATH system variable. 
browser = webdriver.Firefox(firefox_profile=profile) 
browser.get('http://website.com') 
browser.maximize_window() 
Các vấn đề liên quan