Java 谷歌登录并没有在Jenkins的工作中使用无头chrome selenium

Java 谷歌登录并没有在Jenkins的工作中使用无头chrome selenium,java,selenium,jenkins,selenium-webdriver,selenium-chromedriver,Java,Selenium,Jenkins,Selenium Webdriver,Selenium Chromedriver,谷歌登录并没有在Jenkins的工作中使用无头chrome selenium 然而,当我在实际的chrome上运行时,这是有效的 dvr.get("https://accounts.google.com/signin/v2/identifier?service=mail&passive=true&rm=false&continue=https%3A%2F%2Fmail.google.com%2Fmail%2Fh%2F141icwbpdm6lq%2F&ss=1&am

谷歌登录并没有在Jenkins的工作中使用无头chrome selenium

然而,当我在实际的chrome上运行时,这是有效的

dvr.get("https://accounts.google.com/signin/v2/identifier?service=mail&passive=true&rm=false&continue=https%3A%2F%2Fmail.google.com%2Fmail%2Fh%2F141icwbpdm6lq%2F&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1&flowName=GlifWebSignIn&flowEntry=ServiceLogin");

dvr.findElement(By.id("identifierId")).click();

dvr.findElement(By.id("identifierId")).clear();

dvr.findElement(By.id("identifierId")).sendKeys("abc@bsr.com");

dvr.findElement(By.xpath("//*[@id=\"identifierNext\"]/span")).click();

dvr.findElement(By.xpath("//*[@id='password']/div[1]/div/div[1]/input")).clear();

dvr.findElement(By.xpath("//*[@id='password']/div[1]/div/div[1]/input")).sendKeys("xyz");

dvr.findElement(By.xpath("//*[@id=\"passwordNext\"]/span/span")).click();
以下错误在无头模式下出现

org.openqa.selenium.NoSuchElementException:没有这样的元素:无法定位元素:{“方法”:“id”,“选择器”:“identifierId”} (会话信息:无头镀铬=76.0.3809.132) (驱动程序信息:chromedriver=2.38.552518(183d19265345f54ce39cbb94cf81ba5f15905011),平台=Mac OS X 10.12.6 x8664)(警告:服务器未提供>任何堆栈跟踪信息) 命令持续时间或超时:0毫秒 有关此错误的文档,请访问: 构建信息:版本:“3.141.59”,修订版:“e82be7d358”,时间:“2018-11-14T08:17:03” 系统信息:主机:'Homebells-MacBook-Pro-2.local',ip:'fe80:0:0:ab:bf46:6f97:5e3c%en0',os.name:'Mac os X',os.arch:'x86_64',os.version:'10.12.6',>java.version:'1.8.0_144' 驱动程序信息:org.openqa.selenium.chrome.ChromeDriver 功能{AcceptInsureCerts:false,acceptSslCerts:false,applicationCacheEnabled:false,browserConnectionEnabled:false,browserName:chrome,>chrome:{chromedriverVersion:2.38.552518(183d19265345f5…,userDataDir:/var/folders/jq/rrf_Qymx39…),cssSelectorsEnabled:true,databaseEnabled:>false,handlesAlerts:true,hasTouchScreen:false,javascriptEnabled:true,locationContextEnabled:true,mobileEmulationEnabled:false,nativeEvents:true,>networkConnectionEnabled:false,pageLoadStrategy:normal,platform:MAC,platformName:MAC,rotatable:false,setWindowRect:true,takesHeapSnapshot:true,>takesScreenshot:true,意外的AlertBehaviour:,未处理的PromptBehavior:,版本:76.0.3809.132,启用WebStorage:true} 会话ID:61f1a8512d345d1abf7ca3b40e345602 ***元素信息:{Using=id,value=identifierId}

下面是启动浏览器的代码

if (browser.equalsIgnoreCase("chrome") && OS == "Mac") {

        System.out.println("==============================================" );
        System.out.println("OS Detected : MAC , Browser Launched : Chrome" );
        System.out.println("==============================================" );

        System.setProperty("Webdriver.chrome.driver", "./lib/chromedriver");

        Boolean headlesschrome = true;

        if (headlesschrome==true) {

            ChromeOptions chromeOptions = new ChromeOptions();
            chromeOptions.addArguments("--headless");
            dvr = new ChromeDriver(chromeOptions);
            dvr.manage().window().fullscreen(); 

        }else if (headlesschrome==false)

        {
            dvr = new ChromeDriver();
            dvr.manage().window().fullscreen();
        }

    }

错误来自选择器,驱动程序无法找到元素,这就是它抛出错误的原因

这是我登录谷歌账户的老例子

By usernameInput = By.xpath("//input[@type='email']");
By passwordInput = By.xpath("//input[@type='password']");
By nextButton = By.xpath("//span[contains(text(),'Next')]");


try {
        driver.get("https://accounts.google.com/signin/v2/identifier?service=mail&passive=true&rm=false&continue=https%3A%2F%2Fmail.google.com%2Fmail%2Fh%2F141icwbpdm6lq%2F&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1&flowName=GlifWebSignIn&flowEntry=ServiceLogin");
        driver.findElement(usernameinput).sendkeys("fill it");
        driver.findElement(nextButton).click();
        Thread.sleep(2000);

        driver.findElement(passwordInput).sendKeys("fill it");
        driver.findElement(nextButton).click();
        Thread.sleep(2000);

} catch (Exception e) {

}

Chrome无头浏览器的屏幕大小不一定与普通Chrome浏览器相同(我认为它是800x600,但我不确定)

尝试添加以下选项:

chromeOptions.addArguments("--start-maximized");
chromeOptions.addArguments("--window-size=1200,800");

显然,将您想要的屏幕大小设置为1200x800。

我曾经在其他google登录页面的headless中遇到过同样的问题。我这样解决了这个问题:

driver.get("https://accounts.google.com/signin/v2/identifier");

    WebElement emailField = driver.findElement(By.id("Email"));
    emailField.sendKeys(login);


    WebElement loginNext = driver.findElement(By.id("next"));
    loginNext.click();

    logger.info("Enter password");
    WebElement passwordField = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("Passwd")));
    passwordField.sendKeys(password);

    WebElement passwordNext = driver.findElement(By.id("signIn"));
    passwordNext.click();

通过添加带有无头chrome的
--user agent
标志,解决了google登录问题

--user agent=“Mozilla/5.0(X11;Linux x86_64)AppleWebKit/537.36(KHTML,类似Gecko)Chrome/85.0.4183.102 Safari/537.36”

Ref:

选择器在实际的chrome中工作,但当我在chrome中运行headless时,它不起作用。@HillHacker-hmmep()非常不受欢迎。最好使用
FluentWait
,声明如下:
Wait Wait=new FluentWait(driver)。withTimeout(Duration.ofSeconds(30))。pollingEvery(Duration.ofSeconds(5))。忽略(NoSuchElementException.class)
,然后像这样使用:
WebElement my_button=Wait.unt(driver1->driver1.findElement(By([…]);my_按钮。click()
您是否尝试使用
selenium.remote.Augmenter
获取屏幕截图以了解页面上显示的内容?当我这样做时,它会显示一个完全空白的屏幕,因此不知道是否有其他人看到了相同的内容。。。