Cannot locate elements using headless mode Selenium

泄露秘密 提交于 2021-02-05 09:29:38

问题


I cannot locate elements using headless mode because of this restriction "All users will have to use google Chrome when accessing our sites." This restriction was added by our admins so users could use only Google chrome.

My code is

@Test(priority = 1)
    public void setupApplication() throws IOException {
        /*
         * open browser (GoogleChrome) and enter user credentials
         */
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--window-size=1920,1080");
        options.addArguments("--disable-gpu");
        options.addArguments("--disable-extensions");
        options.setExperimentalOption("useAutomationExtension", false);
        options.addArguments("--proxy-server='direct://'");
        options.addArguments("--proxy-bypass-list=*");
        options.addArguments("--start-maximized");
        options.addArguments("--headless");

        driver = new ChromeDriver(options);
        driver.get("link"); 
        log.info("Launching chrome browser");

        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile, new File("C:/Automation Testing/scr3.png"));
    }

Unfortunately I cannot show our link. My question is how to bypass this and find elements?

Thanks in advance! enter image description here


回答1:


Update

if you wish to bypass the headless agent footprint attach following argument:

--user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.50 Safari/537.36

Note: any version you apply to the user-agent argument will be displayed in the request header information.


...or speak with the 'admins' of your project so they can include the headless chrome agent to the white-list.

Here is a normal agent information from chrome:

User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36

Here is the headless chrome

User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/69.0.3497.100 Safari/537.36

As you can see the headless chrome agent is called: HeadlessChrome



来源:https://stackoverflow.com/questions/52589782/cannot-locate-elements-using-headless-mode-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!