4

I am trying to use selenium with Chrome to browse a website that uses javascript.

I installed:

However, I cannot make this work:

from selenium import webdriver

service_args = ['--verbose']
service_log_path = "{}/chromedriver.log".format('/projects/')

driver = webdriver.Chrome('/path/to/chromedriver', service_args=service_args, service_log_path=service_log_path)

I get this error:

WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
  (Driver info: chromedriver=2.13.307649 (bf55b442bb6b5c923249dd7870d6a107678bfbb6),platform=Linux 3.13.0-30-generic x86_64)

Here is my log file: What is the Dev tools issue?

[0.973][INFO]: COMMAND InitSession {
   "desiredCapabilities": {
      "browserName": "chrome",
      "chromeOptions": {
         "args": [  ],
         "extensions": [  ]
      },
      "javascriptEnabled": true,
      "platform": "ANY",
      "version": ""
   }
}
[0.980][INFO]: Populating Preferences file: {
   "alternate_error_pages": {
      "enabled": false
   },
   "autofill": {
      "enabled": false
   },
   "browser": {
      "check_default_browser": false
   },
   "distribution": {
      "import_bookmarks": false,
      "import_history": false,
      "import_search_engine": false,
      "make_chrome_default_for_user": false,
      "show_welcome_page": false,
      "skip_first_run_ui": true
   },
   "dns_prefetching": {
      "enabled": false
   },
   "profile": {
      "content_settings": {
         "pattern_pairs": {
            "https://*,*": {
               "media-stream": {
                  "audio": "Default",
                  "video": "Default"
               }
            }
         }
      },
      "default_content_settings": {
         "geolocation": 1,
         "mouselock": 1,
         "notifications": 1,
         "popups": 1,
         "ppapi-broker": 1
      },
      "password_manager_enabled": false
   },
   "safebrowsing": {
      "enabled": false
   },
   "search": {
      "suggest_enabled": false
   },
   "translate": {
      "enabled": false
   }
}
[0.982][INFO]: Populating Local State file: {
   "background_mode": {
      "enabled": false
   },
   "ssl": {
      "rev_checking": {
         "enabled": false
      }
   }
}
[0.988][INFO]: Launching chrome: /usr/bin/chromium-browser --disable-background-networking --disable-client-side-phishing-detection --disable-component-update --disable-default-apps --disable-hang-monitor --disable-prompt-on-repost --disable-sync --disable-web-resources --enable-logging --ignore-certificate-errors --load-extension=/tmp/.com.google.Chrome.Wgxcag/internal --log-level=0 --metrics-recording-only --no-first-run --password-store=basic --remote-debugging-port=12948 --safebrowsing-disable-auto-update --safebrowsing-disable-download-protection --test-type=webdriver --use-mock-keychain --user-data-dir=/tmp/.com.google.Chrome.Fqyvg3 data:,
[0.991][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[0.999][WARNING]: PAC support disabled because there is no system implementation
[1.113][DEBUG]: DevTools request failed
[1.165][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.166][DEBUG]: DevTools request failed
[1.217][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.217][DEBUG]: DevTools request failed
[1.268][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.269][DEBUG]: DevTools request failed
[1.319][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.320][DEBUG]: DevTools request failed
[1.370][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.371][DEBUG]: DevTools request failed
[1.422][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.423][DEBUG]: DevTools request failed
[1.474][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.475][DEBUG]: DevTools request failed
[1.526][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.527][DEBUG]: DevTools request failed
[1.577][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.578][DEBUG]: DevTools request failed
[1:1:0123/165812:ERROR:image_metadata_extractor.cc(111)] Couldn't load libexif.
[1.629][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.630][DEBUG]: DevTools request failed
[6845:6845:0123/165812:ERROR:browser_main_loop.cc(209)] Gtk: cannot open display: 
[1.680][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.681][DEBUG]: DevTools request failed
[1.731][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.735][DEBUG]: DevTools request failed
[1.786][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.787][DEBUG]: DevTools request failed
[1.837][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.838][DEBUG]: DevTools request failed
[1.889][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.891][DEBUG]: DevTools request failed
[1.942][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.944][DEBUG]: DevTools request failed
[1.995][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.997][DEBUG]: DevTools request failed
Michael
  • 8,357
  • 20
  • 58
  • 86
  • might be useful https://devblog.supportbee.com/2014/10/27/setting-up-cucumber-to-run-with-Chrome-on-Linux/ – Padraic Cunningham Jan 23 '15 at 22:39
  • my first sanity check is to always try to start up chromium and make sure it isn't giving me first-use troubles before trying out selenium. all good there? – kiminoa Jan 23 '15 at 23:40
  • @kiminoa how to check chromium by itself? I tried to run chromium-browser directly and I get two errors: Couldn't load libexif AND Gtk: cannot open display – Michael Jan 24 '15 at 00:31
  • @PadraicCunningham I tried to follow the blog post but it did not change anything. – Michael Jan 24 '15 at 01:30
  • I do not know if it's important but I am on Ubuntu server so there is no display. It can be related to that? – Michael Jan 24 '15 at 01:44
  • if you want to do headless testing PhantomJS is a good option, or http://fgimian.github.io/blog/2014/04/06/headless-browser-testing-with-chrome-and-firefox/ – Padraic Cunningham Jan 24 '15 at 01:59

1 Answers1

4

You can use Xvfb with the help of pyvirtualdisplay to run your chrome automation on a virtual display, since you don't have a real one:

from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=0, size=(800, 600))
display.start()

browser = webdriver.Chrome()
browser.get('http://www.google.com')
print browser.title
browser.quit()

display.stop()

See also:


Aside from that, switching to PhantomJS is also an option:

from selenium import webdriver

browser = webdriver.PhantomJS()
browser.get('http://www.google.com')
print browser.title
browser.quit()
Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Thanks for the response. Do you know whether there is a big difference in terms of performance/simplicity/overhead to use one solution over the other? – Michael Jan 25 '15 at 00:17
  • @YAmikep I guess it would depend on your particular case - measure :) Thanks. – alecxe Jan 25 '15 at 01:37
  • Sorry, I was trying the PhantomJS option and it seems to work even though I still have to figure out how to well use it with websites that use ajax requests heavily. Thanks for your help. – Michael Feb 03 '15 at 15:59
  • It looks like chromedriver started a browser at the selenium client request but the browser died because the DISPLAY environment variable did not point to an X server. I would run "echo $DISPLAY" before starting chromedriver to make sure it has a string such as ":0", if you launch it in a terminal window of your X session. – eel ghEEz Mar 28 '15 at 03:14
  • Also, I figured that chromedriver guesses the location of the browser with the following logic: if a directory entry "chrome" exists in the same directory that contains chromedriver, execute that entry. As I put chromedriver in /opt/google and had the browser in /opt/google/chrome/, this logic failed to even start the browser. To solve this, I moved chromedriver to /opt/google/chrome/. – eel ghEEz Mar 28 '15 at 03:15
  • @eelghEEz that depends on your local PATH configuration. It searches for the binary in each directory as specified in PATH. see: https://seleniumhq.github.io/selenium/docs/api/py/_modules/selenium/webdriver/firefox/firefox_binary.html – Corey Goldberg Jun 25 '17 at 19:37