Why doesn't instagram work with Selenium headless Chrome?

Viewed 2278

I'm trying to build an insta bot that works headless, but it don't seem to find the username, password columns (i.e NoSuchElementException).

I tried to run this code to troubleshoot. (which basicaly opens the ig homepage and screenshots it)

from selenium import webdriver
from time import sleep

options = webdriver.ChromeOptions()
options.headless = True
options.add_argument("--window-size=1920,1080")
browser = webdriver.Chrome(options=options)
browser.get("https://www.instagram.com")
browser.get_screenshot_as_file(f"screenshot.png")

and i got these screenshots basically saying 'error, retry after several minutes' in french

I tried finding the 'connectez-vous' button thru selenium, but every xpath i try doesn't work, and it's impossible to find it thru f12

The bot will be later uploaded to pythonanywhere so i can run it in the cloud (so if you think i might run into some other problems you can let me know)

What do you suggest me to do?

1 Answers
from selenium import webdriver
from time import sleep

options = webdriver.ChromeOptions()
#options.headless = True
options.add_argument("--window-size=1920,1080")
options.add_argument("--headless")
options.add_argument("--disable-gpu")
options.add_argument(
    "user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")
browser = webdriver.Chrome(options=options)
browser.get("https://www.instagram.com")
sleep(5)
#browser.refresh()
browser.get_screenshot_as_file(f"screenshot.png")

For headless chrome , useragent is set as chromeheadless or something , this makes instagram to detect that you are using headless chrome.

You can vent this by specifying hardcoded useragent,

open a normal chrome , goto network tab , open request header and copy the user agent part and replace in your code

Headless browser detection

Related