13

Though I knew people asked this question but I didn't find suitable answer so I asked again. I used PhantomJS to scrape web site, but it is very slow on Mac, so I want to tru Chrome but Chrome show browser that is is bad, can I hide it? I tried code as below, that still show a small browser window..

browser = webdriver.Chrome()
browser.set_window_position(0, 0)
browser.set_window_size(0, 0)
mikezang
  • 2,291
  • 7
  • 32
  • 56
  • This sounds like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What are you trying to accomplish? PhantomJS and Selenium are poor tools for scraping websites. – ChrisGPT was on strike Feb 27 '17 at 02:55
  • I want to hide Chrome browser, is it possible? – mikezang Feb 27 '17 at 03:30
  • My point is that if your goal is actually to scrape websites, launching an invisible Chrome window is a bad way to approach it. Forget about hiding the browser window, forget about Chrome and Selenium; what are you trying to _accomplish_? – ChrisGPT was on strike Feb 27 '17 at 03:33
  • Do you want to tell me that Chrome is not headless browser? – mikezang Feb 27 '17 at 03:48
  • @Chris There are many reasons you'd scrape using selenium/phantomjs, I'd agree not the first choice but sometimes the only and most reliable way... you can see multiple posts/discussions on this issue... – FancyDolphin Feb 27 '17 at 04:05

5 Answers5

25

Google announced in 4/2017 you can run in headless.

https://developers.google.com/web/updates/2017/04/headless-chrome

chrome_options = Options()
# Chrome v75 and lower:
# chrome_options.add_argument("--headless") 
# Chrome v 76 and above (v76 released July 30th 2019):
chrome_options.headless = True

chrome_options.binary_location = '/Applications/Google Chrome   Canary.app/Contents/MacOS/Google Chrome Canary'  
driver = webdriver.Chrome(executable_path=os.path.abspath(“chromedriver"),   chrome_options=chrome_options)

Few things you should make sure

  • If using Mac/Linux then chrome version should be minimum 59
  • If using Windows then chrome version should be minimum 60
  • Use the latest chromedriver as well to make sure you don't have compatibility issue
sealabr
  • 1,593
  • 3
  • 19
  • 28
18

Try This!

https://beomi.github.io/2017/09/28/HowToMakeWebCrawler-Headless-Chrome/

options = webdriver.ChromeOptions()
options.add_argument('headless')
options.add_argument('window-size=1920x1080')
options.add_argument("disable-gpu")
# OR options.add_argument("--disable-gpu")

driver = webdriver.Chrome('chromedriver', chrome_options=options)
Max Lee
  • 241
  • 1
  • 5
  • 14
16

REF: how-could-i-start-a-selenium-browserlike-firefox-minimized

You can move browser window over the monitor, like this:

driver.set_window_position(-10000,0)

Community
  • 1
  • 1
Beomi
  • 1,697
  • 16
  • 18
  • well, I got it by driver.set_window_position(-10000, 0). thanks a lot! – mikezang Feb 27 '17 at 04:53
  • When chrome opens a tab, it is still displayed on mac at the far left – whackamadoodle3000 Aug 10 '17 at 18:23
  • its a workarround, do this: chrome_opt = Options() chrome_opt.add_argument("--headless") driver = webdriver.Chrome(options=chrome_opt) – Marcelo Guedes Jan 04 '23 at 21:40
  • @MarceloGuedes it is easly detectable to use chrome as 'headless' mode. You might be banned from websites where block bots. If you really need headless mode, try this: https://github.com/ultrafunkamsterdam/undetected-chromedriver – Beomi Jan 05 '23 at 08:01
1

I think it will work.

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('headless')
chrome_options.add_argument('window-size=1920x1080')
chrome_options.add_argument("disable-gpu")
driver = webdriver.Chrome('chromedriver', chrome_options=chrome_options)

driver.get('http://google.com')
print(driver.title)
driver.implicitly_wait(3)
driver.get_screenshot_as_file('googleHomePage.png')

driver.quit()
Jawad
  • 166
  • 7
0
chrome_opt = Options()
chrome_opt.add_argument("--headless")
driver = webdriver.Chrome(options=chrome_opt)
Marcelo Guedes
  • 1,419
  • 11
  • 10