11

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,081
  • 7
  • 30
  • 50
  • 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. – Chris 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_? – Chris 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

4 Answers4

24

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,488
  • 3
  • 16
  • 26
17

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,619
  • 15
  • 17
14

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)
Henry Lee
  • 171
  • 1
  • 4
  • 13
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