如何使用 Selenium WebDriver 在 Firefox 中設定代理?
我們可以使用 Selenium webdriver 在 Firefox 中設定代理。即使存在多層網路,代理伺服器也允許使用者為測試目的訪問應用程式的 URL。
可以在 FirefoxOptions 類的幫助下設定 Firefox 中的代理。埠資訊和代理伺服器主機將被新增到此類。還可以透過在 FirefoxProfile 類中配置 Firefox 配置檔案來完成代理伺服器的設定。
示例
使用 FirefoxOptions 的程式碼實現
import org.openqa.selenium.Proxy; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxOptions; public class ConfigureProxy { public static void main(String[] args) { //object of Proxy Proxy p = new Proxy(); //adding host and port p.setHttpProxy("<HOST:PORT>"); p.setSslProxy("<HOST:PORT>"); p.setSslProxy("<HOST:PORT>"); p.setFtpProxy("<HOST:PORT>"); //object of FirefoxOptions FirefoxOptions o = new FirefoxOptions(); o.setCapability("proxy", p); //passing Firefox option to webdriver object WebDriver driver = new FirefoxDriver(o); //url launch driver.get("https://tutorialspoint.tw/index.htm"); //browser close driver.close(); } }
示例
使用 FirefoxOptions 的程式碼實現
import org.openqa.selenium.Proxy; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxProfile; public class ConfigureProxyProfile { public static void main(String[] args) { //object of Proxy Proxy p = new Proxy(); //adding host and port p.setHttpProxy("<HOST:PORT>"); p.setSslProxy("<HOST:PORT>"); p.setSslProxy("<HOST:PORT>"); p.setFtpProxy("<HOST:PORT>"); //object of FirefoxProfile FirefoxProfile pl = new FirefoxProfile(); pl.setProxyPreferences(p); //passing Firefox profile to webdriver object WebDriver driver = new FirefoxDriver(pl); //url launch driver.get("https://tutorialspoint.tw/index.htm"); //browser close driver.close(); } }
廣告