Selenium打开chrome浏览器而不是网页

时间:2015-11-04 10:00:52

标签: selenium-webdriver

我试图使用selenium网格在三个浏览器中打开一个网站。当我运行我的脚本,Firefox和工作正常。但是Chrome浏览器正在打开"数据:,"没有URL。 Selenium版本2.47 Chromedriver.exe 2.20 Chrome 46.0 谁能告诉我为什么?

@Parameters("browser")
   @BeforeTest
   public void launchapp(String browser) throws MalformedURLException
   {
      String URL = "http://www.tutorialspoint.com/selenium/selenium_grids.htm";
      if (browser.equalsIgnoreCase("firefox"))
      {
         System.out.println(" Executing on FireFox");
         String Node = "http://10.101.7.220:5555/wd/hub";
         DesiredCapabilities cap = DesiredCapabilities.firefox();
         cap.setBrowserName("firefox");

         driver = new RemoteWebDriver(new URL(Node), cap);

         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

         // Launch website
         driver.navigate().to(URL);
         driver.manage().window().maximize();
      }
      else if (browser.equalsIgnoreCase("ie"))
      {
         System.out.println(" Executing on IE");

         System.setProperty("webdriver.ie.driver","IEDriverServer.exe");
        DesiredCapabilities capabilities = DesiredCapabilities
                .internetExplorer();

        capabilities
        .setCapability(
                InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,
                true);

        capabilities.setCapability("ignoreZoomSetting", true);
        capabilities.setCapability("nativeEvents", false);
         //driver = new RemoteWebDriver(new URL(Node), capabilities);
        driver= new InternetExplorerDriver(capabilities);
         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

         // Launch website
         driver.navigate().to(URL);
         driver.manage().window().maximize();
      }
      else if (browser.equalsIgnoreCase("chrome")) 
      {
           System.out.println("Running Chrome");

        System.setProperty("webdriver.chrome.driver", "C:\\Users\\mob150003576\\Downloads\\chromedriver.exe");

         driver = new ChromeDriver();
      }
      else
      {
         throw new IllegalArgumentException("The Browser Type is Undefined");
      }
   }

   @Test
   public void sample()
   {
       driver.findElement(By.xpath("//a[text()=' Home']")).click();

   }`

1 个答案:

答案 0 :(得分:0)

我在您的代码中注意到的一件事是您不使用RemoteWebDriver来运行IE或Chrome测试。代码可以简化为:

            String URL = "http://www.tutorialspoint.com/selenium/selenium_grids.htm";            
            System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
            System.setProperty("webdriver.ie.driver", "IEDriverServer.exe");
            DesiredCapabilities caps = null;

            switch(browser){
            case "chrome" : caps = DesiredCapabilities.chrome();
            break;
            case "firefox" : caps = DesiredCapabilities.firefox();
            break;
            case "ie" : caps = DesiredCapabilities.internetExplorer(); 
            caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
            break;
            } 

            driver = new RemoteWebDriver(new URL("grid string"), caps);
            driver.navigate().to(URL);
            driver.manage().window().maximize();

请根据您的PC更新浏览器驱动程序的路径,并添加所需的特定功能。