13

When I run test script in headless mode chrome browser, element link is not visible, is not able to do linkElement.click(). in head mode is everything OK. All other info are in stacktrace. Anyone knows what to do, please?

StackTrace:

ERROR occurred: Message: element not visible
(Session info: headless chrome=60.0.3112.90)
(Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 6.1.7601 SP1 x86_64)
Traceback (most recent call last):
File "C:\nik-x.py", line 148, in main
func(nik)
File "C:\lib\support.py", line 121, in wrapper
raise ret
File "C:\lib\support.py", line 108, in newFunc
res[0] = func(*args, **kwargs)
File "C:\testcases\nik-1003.py", line 37, in testcase
i.click()
File "C:\Python36\lib\site-packages\selenium\webdriver\remote\webelement.py", line 7
7, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Python36\lib\site-packages\selenium\webdriver\remote\webelement.py", line 4
93, in _execute
return self._parent.execute(command, params)
File "C:\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 25
6, in execute
self.error_handler.check_response(response)
File "C:\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line
194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
(Session info: headless chrome=60.0.3112.90)
(Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 6.1.7601 SP1 x86_64)

Here is my piece of code:
icons = nik.elementLeftmenuSportIcons() for i in icons[:-1]: try: i.click()

HTML from testing page: <a href="#" class="default b_futbal gaPageEventElement" data-ga-cat="Sporty" data-ga-action="futbal"> <span class="left-menu-only-large-res">Futbal</span> </a>

Ghost
  • 309
  • 1
  • 4
  • 10
  • The error stack trace says it all `ElementNotVisibleException: Message: element not visible` either you have to wait for the element to be visible or you have to bring the element within the Viewport to be clickable. – undetected Selenium Aug 08 '17 at 08:58
  • That could be some actual difference in JS also. Because a JS might be changing attributes. Instead of clicking the element, get the element and print all its properties, specially coordinates and all. Then see what differences are there – Tarun Lalwani Aug 08 '17 at 09:50
  • @ DebanjanB Thank you, I tried to add wait(10), switch_to_frame, window, etc, but I have still the same problem. – Ghost Aug 09 '17 at 13:39

6 Answers6

16

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.

powerpete
  • 2,663
  • 2
  • 23
  • 49
2

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));
LoRe
  • 121
  • 2
  • 5
1

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)}
1

options.AddArguments("--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36");

Adding this helped me

Shubham Gaikwad
  • 553
  • 5
  • 4
1

Adding this here as a reference for the future, as I faced the same problem. The problem was that the website I was trying to access banned my IP and I was using PROXY_TOOL to access it.

In Full mode, it worked fine, but in Headless mode, it showed that it was unable to find an element. So, I took a screenshot with this function before it showed that it was unable to find an element. (Python 3.9.5)

    def get_screenshot(self, filename):
    screenshot_bytes = self.driver.get_screenshot_as_png()
    with open(filename, "wb") as file:
        file.write(screenshot_bytes)

After saving the screenshot I saw that site was giving an IP blocked error. So, I used PROXY_IP="127.0.0.1", PROXY_PORT=9666 as arguments. On which PROXY_TOOL was serving the proxy access. And it worked.

options.add_argument("--proxy-server=socks5://" + str(PROXY_IP) + ":" + str(PROXY_PORT))

Use the screenshot function to see what is visible on the website in headless mode. See what kind of problem there is if the element is not visible use window size adjustment to make it visible. And adjust according. This might not be the right answer but it is directed to the same problem.

Fahim
  • 61
  • 1
  • 5
0

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.