DucoVDS
DucoVDS

Reputation: 31

Is there a way to use a selenium webdriver without showing the browser?

I tried searching for a solution on this site because there is a question that is almost the same as mine. This sadly this didn't work for me. The code below is what I have right now... Is it possible to start the webdriver without actually showing the process?

# Options
options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')


# Gegevens
password = input("Please give us a password you want to use for all your account(s): ")


# Start de driver
url = 'https://twitter.com/?lang=en-gb'
driver = webdriver.Chrome('/Users/ducov/Downloads/chromedriver')
driver = webdriver.Chrome(chrome_options=options)
driver.set_window_size(1600, 800)
driver.get(url)

Edit: I fixed it by replacing with the code in the answer:

driver = webdriver.Chrome('/Users/ducov/Downloads/chromedriver')
driver = webdriver.Chrome(chrome_options=options)

I still get an error:

C:/Users/ducov/PycharmProjects/bot/app.py:18: DeprecationWarning: use options instead of chrome_options
  driver = webdriver.Chrome('/Users/ducov/Downloads/chromedriver', chrome_options=options)

But I don't think that matters

Upvotes: 0

Views: 713

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193058

You were close enough.

First of all, chrome_options is deprecated now and you have to use optionsinstead.

  • Implementation:

    if chrome_options:
        warnings.warn('use options instead of chrome_options', DeprecationWarning)
        options = chrome_options
    

Second, you want to initialize a single Chrome browsing context only, so you need to pass both the arguments within a single ChromeDriver / Chrome initializer as follows:

# Options
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')

# Gegevens
password = input("Please give us a password you want to use for all your account(s): ")

# Start de driver
url = 'https://twitter.com/?lang=en-gb'
driver = webdriver.Chrome(executable_path='/Users/ducov/Downloads/chromedriver', options=chrome_options)
driver.set_window_size(1600, 800)
driver.get(url)

Upvotes: 1

Elder Frog
Elder Frog

Reputation: 96

You should put execute_path and chrome_options in same line.

driver = webdriver.Chrome('/Users/ducov/Downloads/chromedriver', chrome_options=options)

ps: in your code, you run 2 Chrome instances, one is webdriver.Chrome('/Users/ducov/Downloads/chromedriver'), and another is webdriver.Chrome(chrome_options=options)

Upvotes: 1

Related Questions