少し掘り下げたところ、機能があることがわかりましたが、公開されていません。そのため、パッチを当てるには便利なモンキー レンチが必要です。この機能が webdriver 呼び出しで完全に公開されるまで、私にとって有効なソリューションを次に示します。
編集: service_args が公開されたようです。プロキシを使用するために Selenium にモンキー パッチを適用する必要はもうありません。使用方法については、@alex-czech の回答を参照してください。
from selenium import webdriver
from selenium.webdriver.phantomjs.service import Service as PhantomJSService
phantomjs_path = '/usr/lib/node_modules/phantomjs/lib/phantom/bin/phantomjs'
# monkey patch Service temporarily to include desired args
class NewService(PhantomJSService):
def __init__(self, *args, **kwargs):
service_args = kwargs.setdefault('service_args', [])
service_args += [
'--proxy=localhost:8080',
'--proxy-type=http',
]
super(NewService, self).__init__(*args, **kwargs)
webdriver.phantomjs.webdriver.Service = NewService
# init the webdriver
self.driver = webdriver.PhantomJS(phantomjs_path)
# undo monkey patch
webdriver.phantomjs.webdriver.Service = PhantomJSService
次の設定も役立ちます。特に、読み込みに非常に時間がかかるプロキシを使用している場合に役立ちます。
max_wait = 60
self.driver.set_window_size(1024, 768)
self.driver.set_page_load_timeout(max_wait)
self.driver.set_script_timeout(max_wait)