BeachCoding
BeachCoding

Reputation: 21

selenium and webdriver manager

I am having issues with selenium and webdriver_manager. This script works on my local machine but when I run it on a Linux(ubuntu) server it errors out. It has a problem with:..

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

Any ideas how to solve this?

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.utils import ChromeType

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get('http://python.org')

print(driver.title)

I am getting the following error

====== WebDriver manager ======
Current google-chrome version is 101.0.4951
Get LATEST chromedriver version for 101.0.4951 google-chrome
Trying to download new driver from https://chromedriver.storage.googleapis.com/101.0.4951.41/chromedriver_linux64.zip
Driver has been saved in cache [/root/.wdm/drivers/chromedriver/linux64/101.0.4951.41]
---------------------------------------------------------------------------
WebDriverException                        Traceback (most recent call last)
Input In [1], in <cell line: 4>()
      2 from selenium.webdriver.chrome.service import Service
      3 from webdriver_manager.chrome import ChromeDriverManager
----> 4 driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

Also gives me this error.

WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

Upvotes: 1

Views: 1152

Answers (1)

BeachCoding
BeachCoding

Reputation: 21

I figured it out after quite a bit of testing.

Add this condition to build webdriver fixed it

if sys.platform == 'win32':
    driver = webdriver.Chrome(ChromeDriverManager().install())
if sys.platform == 'linux':
    driver = driver = webdriver.Chrome(service=s, options=chrome_options)
    driver.maximize_window()

final code:

import sys
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
CHROMEDRIVER_PATH = '/usr/local/bin/chromedriver'
s = Service(CHROMEDRIVER_PATH)
WINDOW_SIZE = "1920,1080"
# Options
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=%s" % WINDOW_SIZE)
chrome_options.add_argument('--no-sandbox')
from time import sleep

if sys.platform == 'win32':
    driver = webdriver.Chrome(ChromeDriverManager().install())
if sys.platform == 'linux':
    driver = driver = webdriver.Chrome(service=s, options=chrome_options)
    driver.maximize_window()
# Get the response and print title
driver.get("https://www.python.org")
print(driver.title)
driver.close()

Upvotes: 1

Related Questions