2017-05-26 150 views
0

我们正在使用BrowserStack作为项目。从Eclipse访问URL时获取org.apache.http.conn.HttpHostConnectException

我们正在测试的门户网站已列入我们的IP列入白名单。

而且我们在代理后面访问互联网。

在运行下面的代码片段:

public class DemoClass { 

public static final String USERNAME = "<Username>"; 
public static final String AUTOMATE_KEY = "<Key>"; 
public static final String URL = "https://" + USERNAME + ":" + AUTOMATE_KEY 
+ "@hub-cloud.browserstack.com/wd/hub"; 

public static void main(String[] args) throws Exception { 
String baseUrl; 
DesiredCapabilities caps = new DesiredCapabilities(); 
caps.setCapability("browser", "IE"); 
caps.setCapability("browser_version", "7.0"); 
caps.setCapability("os", "Windows"); 
caps.setCapability("os_version", "XP"); 
caps.setCapability("browserstack.debug", "true"); 
caps.setCapability("browserstack.local", "true"); 
System.getProperties().put("http.proxyHost", "<Proxy URL>"); 
System.getProperties().put("http.proxyPort", "<Proxy Port>"); 
WebDriver driver = new RemoteWebDriver(new URL(URL), caps); 
.... 

我们正在以下错误:

Exception in thread "main" 
org.openqa.selenium.remote.UnreachableBrowserException: Could not start a 
new session. Possible causes are invalid address of the remote server or 
browser start-up failure. 
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown' 
System info: host: '<HOSTNAME>', ip: '<HOST IP>', os.name: 'Windows 
7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_131' 
Driver info: driver.version: RemoteWebDriver 
at 
org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:658) 
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:250) 
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:236) 
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:137) 
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:174) 
at demopackage.DemoClass.main(DemoClass.java:31) 
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to hub- 
cloud.browserstack.com:443 [hub-cloud.browserstack.com/5.255.92.202] failed: 
Connection refused: connect 
......... 

如何解决这个问题?

+0

您能否考虑更新我使用的Selenium版本?谢谢 – DebanjanB

+0

它看起来像你提到的代理细节没有帮助。我注意到您已将Hub网址指定为HTTPS。你有没有尝试切换到HTTP并检查它是否工作?最后,您还可以尝试按如下方式对Hub网址进行卷曲:curl -x -L https://hub.browserstack.com/wd/hub/status。试试HTTP和HTTPS。这将有助于确认您是否可以连接到BrowserStack的Selenium Hub。 –

回答

0

这里是回答你的问题:

我din't发现任何显著的问题在你的代码块这样。但这里有您需要照顾几点:

  1. 当你WebDriver driver = new RemoteWebDriver(new URL(URL), caps);记得import java.net.URL;
  2. 我观察你处理基地例外,因为public static void main(String[] args) throws Exception而你可以考虑public static void main(String[] args) throws MalformedURLException
  3. 要精确您提到了We are using BrowserStack for a project,但您尚未提及是否使用BrowserStack AutomationBrowserStack Running local tests
  4. 假设您使用的是BrowserStack Automation,则需要从代码中删除caps.setCapability("browserstack.local", "true");
  5. 因为您正在使用BrowserStack Running local tests您需要在代码中提及caps.setCapability("browserstack.local", "true");
  6. 假设你正在使用BrowserStack Automation,这里是你自己的代码块用一些简单的调整成功地执行在“BrowserStack自动化”一起:

    package SeleniumGrid; 
    
    import org.openqa.selenium.WebDriver; 
    import org.openqa.selenium.remote.DesiredCapabilities; 
    import org.openqa.selenium.remote.RemoteWebDriver; 
    
    import java.net.MalformedURLException; 
    import java.net.URL; 
    
    public class Q44196893_IE_BrowserStack { 
    
    public static final String USERNAME = "<Username>"; 
    public static final String AUTOMATE_KEY = "<Key>"; 
    public static final String URL = "https://" + USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub"; 
    
    
    public static void main(String[] args) throws MalformedURLException { 
    
    
        String baseUrl; 
        DesiredCapabilities caps = new DesiredCapabilities(); 
        caps.setCapability("browser", "IE"); 
        caps.setCapability("browser_version", "7.0"); 
        caps.setCapability("os", "Windows"); 
        caps.setCapability("os_version", "XP"); 
        caps.setCapability("browserstack.debug", "true"); 
        System.getProperties().put("http.proxyHost", "<Proxy URL>"); 
        System.getProperties().put("http.proxyPort", "<Proxy Port>"); 
        WebDriver driver = new RemoteWebDriver(new URL(URL), caps); 
        driver.get("http://google.com/"); 
        System.out.println("Title is : "+driver.getTitle()); 
        driver.quit(); 
    
    } 
    
    } 
    

让我知道如果这个回答你的问题。

0

在系统变量下设置您的代理_JAVA_OPTIONS,它将开始工作。我遇到了同样的问题,并通过为http和https提供_JAVA_OPTIONS代理来解决问题。