Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use `element.click()` on Selenium with Chrome even on headless mode?

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()
like image 313
Blaszard Avatar asked May 14 '18 09:05

Blaszard


People also ask

Is it possible to run Google Chrome in headless mode with extensions?

You can run Chrome with extensions headless using Xvfb. Use chrome-remote-interface (or another Chrome Debug Protocol client) to trigger the screenshot.

How do I run Selenium scripts in headless Chrome?

You can run Google Chrome in headless mode simply by setting the headless property of the chromeOptions object to True. Or, you can use the add_argument() method of the chromeOptions object to add the –headless command-line argument to run Google Chrome in headless mode using the Selenium Chrome web driver.

Can we take screenshots in headless browser?

Headless browsers are fully capable of taking screenshots, which is very useful in troubleshooting failures or faulty scripts. Using additional libraries and tools, it is also possible to automate visual comparisons.

Does Selenium support headless browser?

Selenium supports headless browser testing using HtmlUnitDriver. HtmlUnitDriver is based on java framework HtmlUnit and is the one of the lightweight and fastest among all headless browser.


1 Answers

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.

like image 148
demouser123 Avatar answered Nov 02 '22 23:11

demouser123