为什么Selenium webdriver没有在远程机器上最大化chrome?

uemypmqf  于 2023-01-03  发布在  Go
关注(0)|答案(1)|浏览(131)

当我在本地机器上运行测试时,它工作正常,但是远程机器没有最大化chrome。因为这个驱动程序找不到屏幕上的元素,所有测试都失败了。我已经尝试了所有推荐的解决方案。下面是我的代码。
公共类BrowserFactory扩展了BaseTest {

public WebDriver prepareDriver() {
    WebDriver driver;

    if (getAveaConfiguration().isRemote().booleanValue()) {
        driver = getRemoteDriver();
        //driver.manage().window().maximize();
        Dimension d = new Dimension(1936,1056);
        driver.manage().window().setSize(d);
        System.out.println("PageSize:" + driver.manage().window().getSize());
    } else {
        driver = getLocalDriver();
        driver.manage().window().maximize();
        System.out.println(driver.manage().window().getSize());
    }
    return driver;
}

private WebDriver getRemoteDriver() {
    WebDriver webDriver;
    DesiredCapabilities cap = getRemoteCapabilities();
    try {
        System.out.println(getAveaConfiguration().getGridURL());
        webDriver = new RemoteWebDriver(new URL(getAveaConfiguration().getGridURL()), cap);

        ((RemoteWebDriver) webDriver).setFileDetector(new LocalFileDetector());
    } catch (MalformedURLException mue) {
        throw new TestException(mue);
    }
    return webDriver;
}

private DesiredCapabilities getRemoteCapabilities() {
    DesiredCapabilities capabilities;
    if ("firefox".equals(getAveaConfiguration().getBrowser())) {
        capabilities = DesiredCapabilities.firefox();

        FirefoxProfile profile = new FirefoxProfile();
        profile.setAcceptUntrustedCertificates(true);
        profile.setAssumeUntrustedCertificateIssuer(false);
        profile.setPreference("browser.privatebrowsing.autostart", true);
        profile.setPreference("network.proxy.type", 0);

        capabilities.setCapability(FirefoxDriver.PROFILE, profile);
        return capabilities;

    } else if ("ie".equals(getAveaConfiguration().getBrowser())) {
        return DesiredCapabilities.internetExplorer();

    } else {
        capabilities = DesiredCapabilities.chrome();

        ChromeOptions options = new ChromeOptions();
        Map<String, Object> prefs = new HashMap<>();
        prefs.put("credentials_enable_service", false);
        prefs.put("profile.password_manager_enabled", false);
        options.setExperimentalOption("prefs", prefs);
        options.addArguments("enable-automation");
        options.addArguments("--headless");
        options.addArguments("--window-size=1920,1080");
        options.addArguments("--no-sandbox");
        options.addArguments("--disable-extensions");
        options.addArguments("--dns-prefetch-disable");
        options.addArguments("--disable-infobars");
        options.addArguments("--disable-dev-shm-usage");
        options.addArguments("--disable-browser-side-navigation");
        options.addArguments("enable-features=NetworkServiceInProcess");
        options.addArguments("--disable-gpu");

        capabilities.setCapability(ChromeOptions.CAPABILITY, options);
        return capabilities;
    }
}

private WebDriver getLocalDriver() {
    switch (getAveaConfiguration().getBrowser()) {
    case "chrome":
        return new ChromeDriver();

//大小写“ie”://返回新的Internet资源管理器驱动程序(getCapabilitiesForIe());返回新的ChromeDriver();} }

i2byvkas

i2byvkas1#

我解决了我的问题,但忘记关闭我的问题。问题是谷歌浏览器文件移动到另一个路径的人。安装浏览器再次和问题解决。

相关问题