8

I'm writing a script to download a set of files on a website using Selenium and its Chrome driver in Python, but I find it disgusting to see the browser opens and gets a focus whenever the program opens a new tab or clicks on the download button.

So I rather want to use headless mode. But then, the element always gets me the following error:

WebDriverException: Message: unknown error: Element ... is not clickable at point (435, 575). Other element would receive the click:

...

(Session info: headless chrome=66.0.3359.139) (Driver info: chromedriver=2.37.544337 (8c0344a12e552148c185f7d5117db1f28d6c9e85),platform=Mac OS X 10.13.4 x86_64)

This error once happened in my non-headless mode, but since I changed the size of the browser by driver.maximize_window(), the error had gone out. This may make it clear the button must be visible on the screen, in order for it to be pushed on Chrome.

But in the headless mode, it might be true that the button is hidden and that's way the element is always unclickable, leading to the error. So it there any way to make the button clickable even on the headless mode?

The relevant code is the following, and the last line causes the error:

def login(driver, url, username, password):
    driver.get(url)
    uname = driver.find_element_by_name("login")
    uname.send_keys(username) 

    passw = driver.find_element_by_name("password")
    passw.send_keys(password)
    submit_button = driver.find_element_by_class_name("button")
    action = webdriver.ActionChains(driver)
    action.move_to_element(submit_button)
    submit_button.click()
Blaszard
  • 30,954
  • 51
  • 153
  • 233

3 Answers3

15

but since I changed the size of the browser by driver.maximize_window()

I faced this issue once when I was working with headless Chrome in Jenkins. The issue was that the browser, in the headless mode didn't open in full resolution. So I added the line to open the browser in specific resolution using

driver.set_window_size(1440, 900)

And then passing the url and getting everything . You can try this approach once to see if this is the issue.

demouser123
  • 4,108
  • 9
  • 50
  • 82
  • Out of all these options: options.add_argument("window-size=2560,1440"), options.add_argument("--window-size=2560,1440"), options.add_argument("start-maximized"), driver.maximize_window() this is the only one that works with headless. Thank you. – Gregor Jan 11 '23 at 08:51
13

I met this problem too. I just fix it like this:

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--window-size=1920,1080')  
chrome_options.add_argument('--headless')
browser = webdriver.Chrome(chrome_options=chrome_options)
Yunfei Tang
  • 496
  • 2
  • 6
  • 11
2

Short answer: Yes.

But there is one difference using clicks in headless and non-headless mode. For non-HEADLESS mode, if you are doing clicks in chromedriver, and at the same time, you perform clicks somewhere on your screen by yourself, then this may interfere with your Selenium automation. This won't be a problem in HEADLESS mode.

Ali Sajjad
  • 3,589
  • 1
  • 28
  • 38