ElementNotVisibleException when use headless Chrome browser

为君一笑 提交于 2019-12-04 02:01:56

I think the problem is, that the Element is really not visible in the default viewbox (600x800) of Headless Chrome.

The window size of the Headless Browser must be set as a Argument when starting chrome. I'm using javascript (I think the API looks similar under python):

var Options = require('selenium-webdriver/chrome').Options;
var options = new Options();
options.addArguments('headless');
options.addArguments('disable-gpu');
options.addArguments('window-size=1200,1100');

browser = builder.forBrowser('chrome').setChromeOptions(options).build();

Additional Info

I'm setup up the window size also by webdriver with browser.manage().window().setSize(1200,1100); But this command is not sufficient in headless chrome. In the non headless variant this is working.

You can do this in two ways as below.

1.Passing window size in chrome options as mentioned below(Before instantiating driver instance):

ChromeOptions options = new ChromeOptions()
options.addArguments("headless");
options.addArguments("window-size=1200,1100");
WebDriver driver = new ChromeDriver(options);

2.Set window size after instantiating chrome driver:

WebDriver webDriver= new ChromeDriver();
webDriver.manage().window().setSize(new Dimension(1200,1100));

options.addArguments('window-size=1200,1100');

Worked for me in headless chrome mode :) Thanks a lot @powerpete

Below are the complete settings for headless chrome in groovy:-

        ChromeOptions options = new ChromeOptions()
        DesiredCapabilities capabilities = DesiredCapabilities.chrome()
        options.addArguments('headless','disable-gpu','--test-type','--ignore-certificate-errors')
        options.addArguments('window-size=1200,1100');
        capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
        capabilities.setCapability(ChromeOptions.CAPABILITY, options)
        driver = {new ChromeDriver(capabilities)}

If changing the window size doesn't work for anyone like me, it may be that the HTML actually changes between headless mode and headed mode.

I had a similar issue, but the headless worked in FireFox and not Chrome. The Xpath for the Chrome element worked only in headed mode. I found out the HTML was changing slightly in Chrome's headless mode.

I fixed this by substituting the Xpath from the element on Chrome with the Xpath from the element on FireFox when using headless mode in Chrome.

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