2017-06-06 65 views
1

我使用Java代碼中的Selenium在Firefox中打開Web應用程序。但是我遇到了Firefox配置文件的問題,因爲當我運行代碼時,Firefox窗口使用新的配置文件打開,所以Web應用程序無法打開,因爲代理設置不同(我應該將IP地址添加到Firefox中,無代理IP) 。我嘗試從我的代碼中獲取默認配置文件,但沒有任何更改。我也嘗試創建新的配置文件,但我不知道如何將IP添加到它。 我改變了代碼,所以我可以手動打開Firefox,然後Selenium在nee標籤中打開應用程序,所以IP將在那裏。但是這也失敗了,代碼仍然打開新窗口。 如果有人能幫忙,我會非常感激。Selenium:使用java代碼打開Firefox,並使用默認配置文件

+0

什麼是您使用的代碼和錯誤是什麼。請在問題陳述中添加這些內容。 – demouser123

+0

你需要打開一個處理代理 –

+0

@LM.O的個人資料的Firefox,你可以考慮顯示你的工作嗎?謝謝 – DebanjanB

回答

1

我們可以用代理值創建一個firefox配置文件,並用該配置文件打開firefox實例。下面的代碼可能會給一些想法。

public static void main(String[] args) 
{ 


     // Create proxy class object 
     Proxy p=new Proxy(); 

     // Set HTTP Port to 7777 
     p.setHttpProxy("localhost:7777"); 

     // Create desired Capability object 
     DesiredCapabilities cap=new DesiredCapabilities(); 


     // Pass proxy object p 
     cap.setCapability(CapabilityType.PROXY, p); 
     System.setProperty("webdriver.gecko.driver", "//PATH"); 
     WebDriver driver=new FirefoxDriver(cap); 

} 

希望這會有所幫助。謝謝。

+0

你能解釋一下端口7777指的是什麼?我在哪裏可以設置服務器IP地址? –

+1

本地主機是服務器IP地址。如果沒有代理然後離開它。所以它看起來像p.setHttpProxy(「172.17.95.1」); –

+0

現在正在工作。非常感謝! –

0

由於您必須使用GeckoDriver才能使用最新的Firefox,因此您可以使用此設置在firefox中爲geckodriver設置代理。

String PROXY = "localhost"; 
int PORT = 8080; 

JSONObject json = new JsonObject(); 
json.addProperty("proxyType", "MANUAL"); 
json.addProperty("httpProxy", PROXY); 
json.addProperty("httpProxyPort", PORT); 
json.addProperty("sslProxy", PROXY); 
json.addProperty("sslProxyPort", PORT); 

DesiredCapabilities cap = new DesiredCapabilities(); 
cap.setCapability("proxy", json); 

GeckoDriverService service =new GeckoDriverService.Builder(firefoxBinary) 
    .usingDriverExecutable(new File("path to geckodriver")) 
    .usingAnyFreePort() 
    .usingAnyFreePort() 
    .build(); 
service.start(); 

// GeckoDriver currently needs the Proxy set in RequiredCapabilities 
driver = new FirefoxDriver(service, cap, cap); 
相關問題