2016-09-30 50 views
1

期間更改代理服務器所以一切工作

fp = webdriver.FirefoxProfile() 
fp.set_preference("network.proxy.type", 1) 
fp.set_preference("network.proxy.http", PROXY_HOST) 
fp.set_preference("network.proxy.http_port", int(PROXY_PORT)) 
fp.update_preferences() 
driver = webdriver.Firefox(firefox_profile=fp) 

但如果驅動程序已經創建,代理無法安裝。 它不起作用

driver = webdriver.Firefox() 
driver.profile.set_preference("network.proxy.type", 1) 
driver.profile.set_preference("network.proxy.http", PROXY_HOST) 
driver.profile.set_preference("network.proxy.http_port", int(PROXY_PORT)) 
driver.profile.update_preferences() 

也是如此。

driver = webdriver.Firefox() 
driver.firefox_profile.set_preference("network.proxy.type", 1) 
driver.firefox_profile.set_preference("network.proxy.http", PROXY_HOST) 
driver.firefox_profile.set_preference("network.proxy.http_port", int(PROXY_PORT)) 
driver.firefox_profile.update_preferences() 

爲什麼?不能理解。 我做錯了什麼?

+0

您是否收到錯誤訊息?如果是這樣,哪一個?網絡驅動程序是否忽略您的新代理設置? –

+0

編號沒有錯誤。一切工作正常。但是這些設置不適用。 –

回答

1

在Firefox中使用WebDriver時,配置文件的使用是一次性的。當驅動程序啓動瀏覽器時,它將配置文件對象寫入磁盤,然後啓動瀏覽器可執行文件。在那之後,瀏覽器沒有機制讀取對WebDriver配置文件對象的任何進一步更改。要更改代理,必須在啓動瀏覽器之前在配置文件中設置設置。