Gaurang Shah
Gaurang Shah

Reputation: 12920

Chrome Headless element not visible

I am facing following error while trying to interact one editbox inside an iframe.

Please note code is working fine, if I don't run that it in headless mode. Issue only occurs when I run in headless mode.

Error:

org.openqa.selenium.ElementNotVisibleException: element not visible

Stacktrace:

org.openqa.selenium.ElementNotVisibleException: element not visible
  (Session info: headless chrome=60.0.3112.113)
  (Driver info: chromedriver=2.33.506092 (733a02544d189eeb751fe0d7ddca79a0ee28cce4),platform=Linux 4.9.51-10.52.amzn1.x86_64 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 21 milliseconds
Build info: version: '2.53.0', revision: '35ae25b1534ae328c771e0856c93e187490ca824', time: '2016-03-15 10:43:46'
System info: host: 'ip-172-31-41-152', ip: '172.31.41.152', os.name: 'Linux', os.arch: 'amd64', os.version: '4.9.51-10.52.amzn1.x86_64', java.version: '1.8.0_45'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.33.506092 (733a02544d189eeb751fe0d7ddca79a0ee28cce4), userDataDir=/tmp/.org.chromium.Chromium.aRCh3q}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=60.0.3112.113, platform=LINUX, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, setWindowRect=true, unexpectedAlertBehaviour=}]
Session ID: ebc4a4ce4a038d2440aedb1f4d952a64
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:327)
    at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:85)
    at com.medexpress.pages.Buyer.CheckoutPage.submitPage(CheckoutPage.java:148)
    at com.medexpress.steps.Order.Submits_the_order(Order.java:120)
    at ✽.And Submits the order(com/medexpress/steps/2oder_checkout.feature:44)

To solve this issue, I am trying to scroll to that Iframe or the element which will bring this iframe in focus. I have tried following things. However nothing is working. it's not scrolling to given element.

Scroll Using Native Events

actions.moveToElement(element).build().perform();

Scroll using JavaScript

Actions actions = new Actions(driver);
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].scrollIntoView()", element);

Scroll to end of page using javascript

js.executeScript("window.scrollTo(0,document.body.scr‌​ollHeight);");

above code is not working and gives following error.

org.openqa.selenium.WebDriverException: unknown error: Runtime.evaluate threw exception: SyntaxError: Invalid or unexpected token

Please note that all the above code is working on chrome, only issue with headless.

Drive Code

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("headless");
chromeOptions.addArguments("disable-gpu");
chromeOptions.addArguments("window-size=1200,1100");
driver = new ChromeDriver(chromeOptions);

Upvotes: 1

Views: 6572

Answers (3)

Increasing the "window-size" fixed the error in the Gulp-Protractor

browserName: 'chrome',
chromeOptions: {
args: [ "--headless", "--disable-gpu", "--window-size=1200,900" ]
}

Upvotes: 0

AC.
AC.

Reputation: 1

Faced similar issue. The following lines of code worked for me -

options.addArguments("--proxy-server='direct://'");
options.addArguments("--proxy-bypass-list=*");

Source - https://github.com/Codeception/CodeceptJS/issues/561

Upvotes: 0

Butrint Haxhibeqiri
Butrint Haxhibeqiri

Reputation: 71

If an element is visible (it has height and width greater than 0), it clicks that element without the need to scroll.

I had the same issue, and fixed it by making the window as FullHD, so just change this line:

chromeOptions.addArguments("window-size=1200,1100");

to

chromeOptions.addArguments("window-size=1980,1080");

Don't know the syntax of Java since I fixed it in C#, but it's worth the try.

Upvotes: 7

Related Questions