想定環境はこんな感じ
FireFox
FireFoxはDriverの個別DLは不要。
FirefoxProfile profile = new FirefoxProfile(); profile.setPreference("network.proxy.type", 1); profile.setPreference("network.proxy.http", "127.0.0.1"); profile.setPreference("network.proxy.http_port", 28080); WebDriver driver = new FirefoxDriver(profile); driver.get("http://localhost/garaponapp/");
こんな感じ。認証ありのProxyの場合にうまくIDパスワードを伝えることは出来ないので、
ローカルに認証をブリッジするProxyでもたてましょう。
chrome
https://code.google.com/p/chromedriver/downloads/list
からchromedriver.exeをDL。DLしたものはPJTOP/driver/に保存。
System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe"); ChromeOptions option = new ChromeOptions(); option.addArguments("--proxy-server=http://127.0.0.1:28080"); option.addArguments("--proxy-bypass-list=localhost"); WebDriver driver = new ChromeDriver(option); driver.get("http://localhost/garaponapp/");
Chromeの動作はFireFoxにくらべるとちょっともっさり。
Proxy設定したときにデフォルトではlocalhostとかに対してもProxyが効いてしまうのでBypass設定も入れてあげる必要あり。
Internet Explorer
http://docs.seleniumhq.org/download/
からDL。64bit版なら
これ
System.setProperty(InternetExplorerDriverService.IE_DRIVER_EXE_PROPERTY, "./driver/IEDriverServer.exe"); DesiredCapabilities capabilities = new DesiredCapabilities(); Proxy proxy = new Proxy(); proxy.setHttpProxy("127.0.0.1:28080"); capabilities.setCapability(CapabilityType.PROXY, proxy); WebDriver driver = new InternetExplorerDriver(capabilities); driver.get("http://localhost/garaponapp/");
IEはめちゃめちゃもっさり。ほんとに耐えられないレベルで遅い。スローモーションかと見間違えるレベル。
そのため
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
の値をかなりでかくしておくのがよい。
また
Caused by: org.openqa.selenium.WebDriverException: Unexpected error launching InternetExplorer. Protected Mode must be set to the same value (enabled or disabled) for allzones. (WARNING: The server did not provide any stacktrace information)
だのなんだのエラーで起こられる場合、インターネットオプションのセキュリティゾーンの保護モードを統一する。 すべてオンまたはすべてオフであればよい。すべてオンにしたほうが良いです。当然ですが。