首页 > 解决方案 > 获取异常:org.openqa.selenium.WebDriverException:未知错误:net::ERR_NAME_NOT_RESOLVED

问题描述

我正在研究 selenium-Cucumber-Gradle 项目我看到当我使用 chrome 浏览器执行我的 UI 自动化脚本时,它只是打开浏览器,甚至我可以看到 url 显示在浏览器地址栏中,但在 2 秒内它退出有一个例外,如下所示。这里的任何建议将不胜感激。我正在使用最新的 chrome 浏览器和 chrome 驱动程序

输出结果 ==============

    Given User navigates to the Rlbs website # RLbys_CardPage_Ui_Step_Test.navigates_to_the_RLby_s_website()
      org.openqa.selenium.WebDriverException: unknown error: net::ERR_NAME_NOT_RESOLVED
  (Session info: chrome=85.0.4183.83)
Build info: version: '4.0.0-alpha-4', revision: 'c2d955b49e'

Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 85.0.4183.83, chrome: {chromedriverVersion: 85.0.4183.87 (cd6713ebf92fa..., userDataDir: C:\Users\xxx~1\AppData\L...}, goog:chromeOption
s: {debuggerAddress: localhost:61816}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileIntera
ctability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:virtualAuthenticators: true}
Session ID: e88071b72a89d636e000150881ccb44a
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:196)
        at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:129)
        at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:53)
        at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:161)
        at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:582)
        at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:282)
        at com.pros.HALbrands.test.stepdefinition.HALCardPage_Ui_Step_Test.user_navigates_to_the_HALRLby_s_website(HALRLbyCardPage_Ui_Step_Test.java:40)
        at ?.User navigates to the HALRLby website(file:src/test/resources/featurefile/HALRLbyCardPage_Ui.feature:5)

我可以知道引发异常的可能是什么问题。

请在下面找到代码片段:

特征文件代码

特点:谷歌搜索功能

场景:验证谷歌搜索功能给定用户导航到谷歌网站当用户在搜索文本字段中输入文本作为卡片时

阶梯防御

 public class Test1
 {

    @Given("User navigates to the google website")
    public void user_navigates_to_the_Google_s_website() {
 
        driver.get("https://www.google.com/");
        
    }

}

------------------------------下面调用浏览器------ ---------

public WebDriver standAloneStepUp(BrowserType bType) throws Exception {
        WebDriver driver;
        
        try {
            oLog.info(bType);

            switch (bType) {

            case CHROME:
                //ChromeBrowser chrome = ChromeBrowser.class.newInstance();
                //WebDriverManager.chromedriver().setup();
                System. setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\src\\main\\resources\\driver\\chromedriver.exe");
                driver = new ChromeDriver();
                return driver;

            case FIREFOX:
//              FirefoxBrowser firefox = FirefoxBrowser.class.newInstance();
//              WebDriverManager.firefoxdriver().setup();
                System. setProperty("webdriver.gecko.driver", System.getProperty("user.dir")+"\\src\\main\\resources\\driver\\geckodriver.exe");
                driver = new FirefoxDriver();
                //driver = new FirefoxDriver(firefox.getFirefoxOptions());
                return driver;


         default:
                throw new NoSutiableDriverFoundException(" Driver Not Found : " + ObjectRepo.reader.getBrowser());
            }
        } catch (Exception e) {
            oLog.equals(e);
            throw e;
        }
    }

标签: seleniumgradleselenium-chromedriver

解决方案


我弄清楚是什么问题。实际上,当您访问尚未向公众开放的站点时,就会发生这种情况。所以分两步解决:

  • 首先我连接到客户端 VPN
  • 虽然连接到客户端 VPN,但我仍然遇到同样的问题,所以在主机文件中添加了几行与 clinet url 以及 ip 地址相关的行

主机文件路径:C:\Windows\System32\drivers\etc\host

10.104.0.38 ida-oa.dev.dp-prv.halproducts.eastus.cloudapp.azure.com

10.104.0.38 order-inte-service-dev.dev.dp-prv.halproducts.astus.cloudapp.azure.com

<ip adress > <client env  url >

推荐阅读