无界面 Chrome 使用 Selenium-Java 在浏览器的普通 UI 模式下运行测试。

huangapple go评论73阅读模式
英文:

Headless chrome using Selenium-Java running tests in normal UI mode of browser

问题

package XYZ;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class Test{
    public static void main(String args[]) {
        System.setProperty("webdriver.chrome.driver",
                "C:\\Users\\TestUser\\Desktop\\SeleniumWorkspace\\ABC\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("headless");
        options.addArguments("window-size=1400,800");       
        options.addArguments("disable-gpu");
        //options.addArguments("--headless", "--disable-gpu", "--window-size=1400,800","--ignore-certificate-errors");
        WebDriver driver = new ChromeDriver(options);    
        driver.get("https://www.google.com");
        System.out.println(driver.getCurrentUrl());
    }
}
英文:

I am trying to run my application in headless mode using Chrome Browser, Selenium and Java. But it open a new chrome instance and start running the script in normal UI mode(can see the execution)

Is there anything wrong in code or browser compatibility.

OS - Windows 10,
Chrome Version - 85.0.4183.121

Below is my code -

package XYZ;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class Test{
	public static void main(String args[]) {
		System.setProperty("webdriver.chrome.driver",
				"C:\\Users\\TestUser\\Desktop\\SeleniumWorkspace\\ABC\\chromedriver.exe");
		ChromeOptions options = new ChromeOptions();
		options.addArguments("headless");
		options.addArguments("window-size=1400,800");		
		options.addArguments("disable-gpu")
		//options.addArguments("--headless", "--disable-gpu", "--window-size=1400,800","--ignore-certificate-errors");
		WebDriver driver = new ChromeDriver(options);	
		driver.get("https://www.google.com");
		System.out.println(driver.getCurrentUrl());
	}
}

答案1

得分: 5

我能看到在代码中传递参数之前,你漏掉了"--"。以下是在运行自动化案例时使用无头 Chrome 的正确代码:

package XYZ;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class Test{
    public static void main(String args[]) {
        System.setProperty("webdriver.chrome.driver",
                "C:\\Users\\TestUser\\Desktop\\SeleniumWorkspace\\ABC\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--headless");     
        options.addArguments("--disable-gpu");
        options.addArguments("--window-size=1400,800");  
        WebDriver driver = new ChromeDriver(options);   
        driver.get("https://www.google.com");
        System.out.println(driver.getCurrentUrl());
    }
}

对我来说,这个代码可以正常工作。我希望这解决了你的问题。

英文:

I can see you are missing "--" before passing the arguments in the code. Below is the correct code to use headless chrome while running your automated cases:

    package XYZ;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class Test{
    public static void main(String args[]) {
        System.setProperty("webdriver.chrome.driver",
                "C:\\Users\\TestUser\\Desktop\\SeleniumWorkspace\\ABC\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--headless");     
        options.addArguments("--disable-gpu");
        options.addArguments("--window-size=1400,800");  
        WebDriver driver = new ChromeDriver(options);   
        driver.get("https://www.google.com");
        System.out.println(driver.getCurrentUrl());
    }
}

For me this is working fine. I hope this solves your probelm.

huangapple
  • 本文由 发表于 2020年9月24日 20:27:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64046452.html
  • google-chrome
  • google-chrome-headless
  • headless
  • java
  • selenium-webdriver
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定