0

As the title says, I'm having trouble with the autofill functionality of Google Chrome. When I run my Python script without the --headless argument, everything goes well: both username and password fields are automatically filled in by Chrome and I'm able to login. However, when I try to run Chrome in headless mode, it doesn't do anything. Is there a way to solve/circumvent this problem? I thought of just using send_keys(), but I'd rather not have my email and password laying in a script on my computer. Thank you!

[EDIT]

This is my code for logging in:

login = WebDriverWait(driver, 10).until( 
            EC.element_to_be_clickable((By.ID, 'login-button')) 
        ) 
driver.find_element_by_id('login-button').click()

I just wait for the element to become clickable and then click on it. As I said before, this completely works when I'm not in headless mode but breaks when I activate it because Chrome doesn't autofill username and password.

[EDIT 2]

I've already passed the user data directory. Everything works well. The problem is when I add the --headless argument, it stops pasting automatically the credentials.

sgrontflix
  • 95
  • 1
  • 1
  • 9
  • 1
    where is your code trials and errors? HTML code of the elements? – Dev Dec 10 '20 at 13:20
  • I didn't try to do anything because I have no idea what to do besides just clicking on the login button, which I achieve by just calling the click() method. I'm testing my script on mi.com. I know that Chrome doesn't autofill in headless mode because I dumped the HTML after clicking on the login button and I saw the error. I'll edit my post with the code I use for signing in. – sgrontflix Dec 10 '20 at 13:44

1 Answers1

0

You would need to pass in the profile of the user who has the autocomplete information.

Here's an answer that might help: https://stackoverflow.com/a/52399027/1387701

Specifically:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

    options = webdriver.ChromeOptions()
    options.add_argument("user-data-dir=C:\\Users\\AtechM_03\\AppData\\Local\\Google\\Chrome\\User Data\\Default")
    driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
    driver.get("https://www.google.com")

However, I'd advise against relying on autocomplete for automated testing. You're better off either passing in username/password information on the command line, through environment variables or via a configuration file.

DMart
  • 2,401
  • 1
  • 14
  • 19
  • I've already passed the user data directory, in fact it works flawlessly when not in headless mode. Why do you advise not to rely on autocomplete? Is it unsafe? Does it break sometimes (like in this case)? – sgrontflix Dec 10 '20 at 21:33
  • I don't think it follows good dev practices is all. Can't programmatically change user/password that way. – DMart Dec 10 '20 at 21:46
  • Yeah, you're absolutely right but I'm still curious to find out why headless mode is so problematic. Maybe it's a bug, I don't know. Do you have any idea? – sgrontflix Dec 12 '20 at 08:17