WebDriverException: unknown error: DevToolsActivePort file doesn't exist while trying to initiate Chrome Browser

前端 未结 29 3437
旧巷少年郎
旧巷少年郎 2020-11-21 07:11

I am trying to launch chrome with an URL, the browser launches and it does nothing after that.

I am seeing the below error after 1 minute:

Unable to         


        
29条回答
  •  时光说笑
    2020-11-21 07:17

    I was facing the same issue recently and after some trial and error it worked for me as well.

    MUST BE ON TOP:

    options.addArguments("--no-sandbox"); //has to be the very first option
    

    BaseSeleniumTests.java

    public abstract class BaseSeleniumTests {
    
        private static final String CHROMEDRIVER_EXE = "chromedriver.exe";
        private static final String IEDRIVER_EXE = "IEDriverServer.exe";
        private static final String FFDRIVER_EXE = "geckodriver.exe";
        protected WebDriver driver;
    
        @Before
        public void setUp() {
            loadChromeDriver();
        }
    
        @After
        public void tearDown() {
            if (driver != null) {
                driver.close();
                driver.quit();
            }
        }
    
        private void loadChromeDriver() {
            ClassLoader classLoader = getClass().getClassLoader();
            String filePath = classLoader.getResource(CHROMEDRIVER_EXE).getFile();
            DesiredCapabilities capabilities = DesiredCapabilities.chrome();
            ChromeDriverService service = new ChromeDriverService.Builder()
                    .usingDriverExecutable(new File(filePath))
                    .build();
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--no-sandbox"); // Bypass OS security model, MUST BE THE VERY FIRST OPTION
            options.addArguments("--headless");
            options.setExperimentalOption("useAutomationExtension", false);
            options.addArguments("start-maximized"); // open Browser in maximized mode
            options.addArguments("disable-infobars"); // disabling infobars
            options.addArguments("--disable-extensions"); // disabling extensions
            options.addArguments("--disable-gpu"); // applicable to windows os only
            options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
            options.merge(capabilities);
            this.driver = new ChromeDriver(service, options);
        }
    
    }
    

    GoogleSearchPageTraditionalSeleniumTests.java

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class GoogleSearchPageTraditionalSeleniumTests extends BaseSeleniumTests {
    
        @Test
        public void getSearchPage() {
            this.driver.get("https://www.google.com");
            WebElement element = this.driver.findElement(By.name("q"));
            assertNotNull(element);
        }
    
    }
    

    pom.xml

    
            
                org.springframework.boot
                spring-boot-starter
            
            
                org.springframework.boot
                spring-boot-starter-test
                test
            
            
                org.seleniumhq.selenium
                selenium-java
                test
            
    
    

提交回复
热议问题