【问题标题】:Selenium can't find WebElements when trying to use headless chromedriver option尝试使用无头 chromedriver 选项时,Selenium 找不到 WebElements
【发布时间】:2019-11-30 08:50:26
【问题描述】:

我正在使用 chromedriver 运行 selenium,一切正常。 最近我尝试同时使用 10 个 chromedrivers,它占用了所有可用内存,所以我尝试使用 ChromeOptions 中的 headless 选项来解决它。

尝试了这些选项:

        options.addArguments("--headless");
        options.addArguments("--disable-gpu");
        options.addArguments("--window-size=1920,1200");
        options.addArguments("--ignore-certificate-errors");

但是当程序使用 --headless 选项运行时,它将不起作用。 (它以前在没有无头的情况下工作)

这就是我设置 chromeDriver 的方式:

private ChromeDriver setupChromeDriver(String proxyAddress, String downloadFilePath){
        try{
            HashMap<String, Object> chromePrefs = new HashMap<>();
            chromePrefs.put("profile.default_content_settings.popups", 0);
            chromePrefs.put("download.default_directory", downloadFilePath);
            ChromeOptions options = new ChromeOptions();
            options.setExperimentalOption("prefs", chromePrefs);
            options.addArguments("--headless");
            options.addArguments("--disable-gpu");
            options.addArguments("--window-size=1920,1200");
            options.addArguments("--ignore-certificate-errors");
            DesiredCapabilities cap = DesiredCapabilities.chrome();
            cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
            cap.setCapability(ChromeOptions.CAPABILITY, options);
            if(!proxyAddress.equals(""))
                cap.setCapability("proxy", setupChromeProxy(proxyAddress));
            ChromeDriverService chromeDriverService = new ChromeDriverService.Builder().build();
            options.merge(cap);
            logger.info("Selenium chrome driver set up with updated download location");
            return new ChromeDriver(chromeDriverService, options);
        }
        catch (Exception e){
            logger.info(e.getMessage());
            logger.info(BaseUtilities.getStackTrace(e));
            e.printStackTrace();
            return null;
        }
    }

我如何找到没有无头选项的元素的示例:

driver.findElement(By.xpath("//input[@id='identifierId']")).sendKeys(dumper.getUsername() + Keys.ENTER);

我使用的是 Windows 10,chrome 版本 75.0.3770.142(官方版本)(64 位), ChromeDriver 75.0.3770.140

试图在类似帖子中找到一些信息,但找不到有效的解决方案。 谢谢。

【问题讨论】:

    标签: java selenium selenium-chromedriver


    【解决方案1】:

    你可以这样尝试

        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--headless");
    
        WebDriver wDriver = new ChromeDriver(chromeOptions);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-10
      • 2016-03-19
      • 2017-04-13
      • 1970-01-01
      • 1970-01-01
      • 2018-11-15
      • 1970-01-01
      • 2021-08-20
      相关资源
      最近更新 更多