【问题标题】:How to start Chrome (both normal & incognito modes) and open URL in remote PC (node) using Selenium Grid in Java?如何使用 Java 中的 Selenium Grid 启动 Chrome(正常模式和隐身模式)并在远程 PC(节点)中打开 URL?
【发布时间】:2017-08-14 12:01:51
【问题描述】:

我正在 Java 中尝试 Selenium Grid,只是想同时启动 Chrome

  • 正常模式和
  • 隐身模式

在远程 PC(节点)中打开 google.com

我已设置集线器 - 节点连接。 我试过这段代码,但它似乎是错误的。

  1. 任何指导如何做到这一点?
  2. 如何使用capability.setCapability()?我找到了一些 example 来启动 IE,只是用 Chrome 替换了 InternetExplorer 这个词......不起作用。

谢谢。

不工作的代码:

System.setProperty("webdriver.chrome.driver" , "C:/Users/chromedriver_win32/chromedriver.exe");

WebDriver driver;
    		 
DesiredCapabilities capability = DesiredCapabilities.chrome();
capability.setCapability(ChromeDriver.BINARY, new File("C:\\Program Files (x86)\\chrome.exe").getAbsolutePath());

driver = new RemoteWebDriver(new URL("http://192.168.0.106:1234/wd/hub"), capability);
driver.get("http://google.com");
System.out.println(driver.getTitle());

【问题讨论】:

    标签: java google-chrome selenium selenium-grid


    【解决方案1】:
    **Normal mode:**
    
    WebDriver driver;
    driver=new ChromeDriver();
    
    **incognito mode:**
    
       WebDriver driver;
    
    System.setProperty("webdriver.chrome.driver","C:/Users/chromedriver_win32/chromedriver.exe");
            ChromeOptions options = new ChromeOptions();
            options.addArguments("-incognito");
            DesiredCapabilities capabilities = DesiredCapabilities.chrome();
            capabilities.setCapability(ChromeOptions.CAPABILITY, options);
            driver=new ChromeDriver(capabilities);
    

    【讨论】:

      【解决方案2】:

      1 - 功能:https://sites.google.com/a/chromium.org/chromedriver/capabilities

      2 - “Icognito Mode”是 Chrome 浏览器的一项功能 - 设置如下:

      DesiredCapabilities capabilities = DesiredCapabilities.chrome();
      ChromeOptions options = new ChromeOptions();
      options.addArguments("-incognito");
      capabilities.setCapability(ChromeOptions.CAPABILITY, options);
      
      // For local usage
      driver = new ChromeDriver(capabilities);
      
      // For HUB usage
      driver = new RemoteWebDriver(new URL("hub url here"), capabilities);
      

      3 - 您需要对指向您的驱动程序可执行文件的系统属性使用反斜杠,如果您使用 JAVA,您还需要使用额外的反斜杠来转义:

      System.setProperty("webdriver.chrome.driver", "C:\\pathto\\my\\chromedriver.exe");
      

      4 - 对于 HUB 使用,每个节点都需要 chromedriver。只需将它放在与 selenium-server-standalone-3.3.1.jar 相同的文件夹中,然后像这样启动节点:

      java -Dwebdriver.chrome.driver="./chromedriver.exe" -jar selenium-server-standalone-3.3.1.jar -role node -nodeConfig nodeConfig.json
      

      当然,您需要一个允许 Chrome 浏览器的 nodeConfig。这是一个例子:

      {
        "capabilities":
        [
          {
            "browserName": "chrome",
            "maxInstances": 5,
            "seleniumProtocol": "WebDriver"
          }
        ],
        "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
        "maxSession": 5,
        "port": 5555,
        "register": true,
        "registerCycle": 5000,
        "hub": "http://localhost:4444",
        "nodeStatusCheckTimeout": 5000,
        "nodePolling": 5000,
        "role": "node",
        "unregisterIfStillDownAfter": 60000,
        "downPollingLimit": 2,
        "debug": false,
        "servlets" : [],
        "withoutServlets": [],
        "custom": {}
      }
      

      【讨论】:

        猜你喜欢
        • 2012-08-05
        • 2022-10-01
        • 1970-01-01
        • 2021-07-11
        • 2016-10-12
        • 2022-11-11
        • 1970-01-01
        • 2021-01-09
        • 1970-01-01
        相关资源
        最近更新 更多