2017-06-06 27 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); 
相关问题