1
votes

Hi guys I'm running some crawling script with Selenium and Python, I want to run the Chrome in headless mode so I set the headless options to true as below

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

options = Options()
options.headless = True
options.add_argument("--start-maximized")
options.add_argument("--window-size=1920,1080")
options.add_argument("--disable-gpu")
options.add_argument("--no-sandbox")

driver = webdriver.Chrome('chromedriver.exe', options=options)

But when running the script, Chrome return the web in mobile version (I've captured screenshot to check the error). Because of this my script cannot run properly

I've try many ways to change it back to desktop website, added arguments like "--window-size=1920,1080", "--start-maximized", etc.. then set browser.maximize_window() and browser.set_window_size(). I also try different chromedriver version but it doesn't work at all

Can anyone help me please? Many thanks.

1

1 Answers

2
votes

Yep, I had a very similar issue. The first thing you need to do is manually identify your user agent, check out this site. For example, your user agent could be a long string describing a Safari browser running on macOS that renders web pages using the WebKit engine.

Now go ahead and add in an option to manually set your user agent

options.add_argument("user-agent=User-Agent: your user agent string here")

An example might look like this:

options.add_argument("user-agent=User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/507.06 Safari/507.06")