HENNGE / arsenic

Async WebDriver implementation for asyncio and asyncio-compatible frameworks
Other
350 stars 53 forks source link

Setting cookie on headless Chrome not working #56

Open Edvard-D opened 5 years ago

Edvard-D commented 5 years ago

For my application I'm having the user authenticate themselves in a visible Chrome browser session and saving the authentication cookie. I need to add that cookie to a different headless session that will be used to do the actual work. I have a manager script that's checking if the authentication is still valid on a regular basis (by trying to get a specific element that only exists when logged in), with it relaunching a visible browser session if the user logs out.

Unfortunately, my application is in a loop of launching a visible session A, getting the cookie, setting the cookie on the headless session B, checking that authentication is still valid, and relaunching the visible session A to re request authentication. This seems to mean that the cookie isn't getting set properly or the headless session isn't able to find the element using get_element.

What's strange is that when I try switching the headless session to a visible session everything's working properly. In this scenario the application is requesting user authentication in visible session A, then taking the cookie from that and adding it to visible session B. Visible session A closes (as it should) and B continues to check that the authentication is still valid. Session A is no longer getting relaunched since session B is able to find the element.

I've looked through the logs to see if there are any sorts of exceptions being thrown, but there aren't (except 'no such element', but that's being handled). One thing I noticed is the following:

Starting ChromeDriver 2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab) on port 57194 Only local connections are allowed. 2019-02-05 15:07.41 request body={"desiredCapabilities": {"browserName": "chrome", "chromeOptions": {"args": ["--headless", "--disable-gpu"]}}} method=POST url=http://localhost:57194/session [0205/150741.616:ERROR:gpu_process_transport_factory.cc(967)] Lost UI shared context.

At the end you can see ERROR:gpu_process_transport_factory.cc(967) Lost UI shared context. No idea if that's related, but I thought it's worth mentioning. Is it possible I'm doing something wrong? If there's anything I need to clarify please let me know.

atom-smasher commented 4 years ago

Similar problem here. This can be confirmed by using headless mode to take a screenshot of a site, such as this one, that you're logged in to. Instead of seeing that you're logged in, the screenshot will show a "Sign in" button.

ojii commented 4 years ago

could this just be the website you're trying to access detecting headless browsers and rejecting them?

atom-smasher commented 4 years ago

Happening on multiple sites, and the user-agent is set to reflect a "normal" browser.

liuhuac commented 4 years ago

Any update? I am hitting the same situation. I am using headless chrome to take a screenshot. It working for visible chrome, but not headless.

dimaqq commented 4 years ago

+label help-wanted

dbaldacchino commented 3 years ago

Running into the same issue. I am trying to automate printing to pdf but the pages are behind a login page. Without a session cookie, it just displays the login page.

loyogoy-g commented 3 years ago

Any solutions ? I think the site detects headless browser