7

I scraping many info from web, and I hope it works on cloud. So I'd like to use colaboratory, but it turned error

WebDriverException                        Traceback (most recent call last)
<ipython-input-35-abcc3b93dfa7> in <module>()
     20 options.add_argument("--start-maximized");
     21 options.add_argument("--headless");
---> 22 driver = webdriver.Chrome('chromedriver', chrome_options=options)
     23 
     24 book  = cd + "/target.xlsx"

/usr/local/lib/python3.6/dist-packages/selenium/webdriver/chrome/webdriver.py in __init__(self, executable_path, port, options, service_args, desired_capabilities, service_log_path, chrome_options, keep_alive)
     71             service_args=service_args,
     72             log_path=service_log_path)
---> 73         self.service.start()
     74 
     75         try:

/usr/local/lib/python3.6/dist-packages/selenium/webdriver/common/service.py in start(self)
     96         count = 0
     97         while True:
---> 98             self.assert_process_still_running()
     99             if self.is_connectable():
    100                 break

/usr/local/lib/python3.6/dist-packages/selenium/webdriver/common/service.py in assert_process_still_running(self)
    109             raise WebDriverException(
    110                 'Service %s unexpectedly exited. Status code was: %s'
--> 111                 % (self.path, return_code)
    112             )
    113 

WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: -6

I read the articles, and it says this works. How can we use Selenium Webdriver in colab.research.google.com? But actually not.

Any Ideas are appreciated.

My option is

options = Options()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-gpu')
driver = webdriver.Chrome('chromedriver', chrome_options=options)

↑ this last sentence makes error

WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: -6

============================================ My entire chart

!sudo apt install unzip
!wget https://chromedriver.storage.googleapis.com/2.37/chromedriver_linux64.zip
!unzip chromedriver_linux64.zip -d /usr/bin/
from google.colab import drive
drive.mount('/content/drive')
!pip install selenium
!pip install openpyxl

then, python script is

cd = "drive/My Drive/doc/業務資料/イーコレ/scrape/*"
import os, subprocess
import sys
sys.path.insert(0,'/usr/lib/chromium-browser/chromedriver')
import selenium
import bs4
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from bs4 import BeautifulSoup
import openpyxl
import time, re, csv, urllib.parse
options = Options()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-gpu')
driver = webdriver.Chrome('chromedriver', chrome_options=options)
Kohei Murakami
  • 339
  • 4
  • 14
  • Am I correct in assuming that you ran `apt-get update` and tried with `--fix-missing` as the error message suggests? – C. Peck Mar 10 '19 at 06:41
  • @C.Peck sorry to be late. Yes! It fetched!!! but, somehow it doesn't install chromedriver. I found the answer, so I will post it – Kohei Murakami Mar 10 '19 at 07:16
  • Does it help if you add the `--disable-gpu` argument? – C. Peck Mar 10 '19 at 09:32
  • Also have you defined your path to chromedriver? It can be added in `webdriver.Chrome()` – C. Peck Mar 10 '19 at 09:35
  • yes, I actually succeeded in local machine. the code is like this(I will write this in the question as well) options = Options() options.add_argument('--headless') options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage') options.add_argument('--disable-gpu') driver = webdriver.Chrome('chromedriver', chrome_options=options) – Kohei Murakami Mar 10 '19 at 11:13
  • You say you succeeded, but "this last sentence makes error". What error? – C. Peck Mar 10 '19 at 11:34
  • I tried to do the same in chrome box without success. This kinds of **operative systems** are **special** and complex frameworks will not work. Also the **status code -6** seems to be caused by the incompatibility between the version of the binarie and chrome version https://stackoverflow.com/a/53537003/3957754. Also could you share us a reproducible minimum example of your code? – JRichardsz Mar 10 '19 at 12:36
  • @JRichardsz Thank you for your reply. I edit the question, after "============================================" is my minimum example. – Kohei Murakami Mar 10 '19 at 15:37

2 Answers2

9
# install chromium, its driver, and selenium
!apt update
!apt install chromium-chromedriver
!pip install selenium
# set options to be headless, ..
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
# open it, go to a website, and get results
wd = webdriver.Chrome('chromedriver',options=options)
wd.get("https://www.website.com")
print(wd.page_source)  # results

I wrap this all into a library

!pip install kora
from kora.selenium import wd
korakot
  • 37,818
  • 16
  • 123
  • 144
  • 1
    @vesszabo Newer Colab version doesn't need `cp...`. So I'll remove it. I only use Colab (I don't have my own linux) – korakot Aug 14 '19 at 12:35
  • Thanks. However "WebDriverException: Message: 'chromedriver' executable needs to be in PATH." So it arises the question: which directory contains chromedriver? – vesszabo Aug 14 '19 at 12:46
  • 2
    @vesszabo It's already at /usr/bin/chromedriver . You can see my test notebook here. https://colab.research.google.com/drive/1LHfo6sW5ZZ5pc8VYEKc0mgavRBFmkyM5 – korakot Aug 14 '19 at 13:36
  • Thanks. I sent access requests (with two different gmail accounts). – vesszabo Aug 14 '19 at 18:38
  • In the meantime I found a working solution. Thanks for your effort. – vesszabo Aug 14 '19 at 19:31
  • 1
    This worked for me. I was using cp earlier and it was showing me permission errors. – lakshyaag Aug 15 '19 at 13:55
  • @korakot this seems to work but I cannot click on any element using the .click() method. – Shell1500 Oct 09 '20 at 20:11
  • I am getting: ImportError: cannot import name 'Literal' from 'typing' (/usr/lib/python3.7/typing.py) when I try to run both your library and the Selenium code. Can you help, please? – user3347814 May 05 '22 at 13:53
  • 1
    @user3347814 Can you share your code? I can run mine without any problem? – korakot May 06 '22 at 11:30
  • 1
    I have notice that I was not the only one with this problem (https://stackoverflow.com/questions/72128879/why-did-selenium-stop-working-today-on-colab?noredirect=1&lq=1), however, it seems just to have disappeared after a while. Selenium is indeed working normally now. Thanks for the attention, thought! – user3347814 May 06 '22 at 13:40
-1

I think this code would work:

!sudo apt install unzip
!wget https://chromedriver.storage.googleapis.com/2.37/chromedriver_linux64.zip
#!unzip chromedriver_linux64.zip -d /usr/bin/
from google.colab import drive
!pip install selenium
!pip install openpyxl
!apt-get update
!apt-get install -y unzip xvfb libxi6 libgconf-2-4
!apt-get install default-jdk 
cd = "drive/My Drive/doc/業務資料/イーコレ/scrape/*"
import os, subprocess
import sys
sys.path.insert(0,'/usr/lib/chromium-browser/chromedriver')
import selenium
import bs4
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from bs4 import BeautifulSoup
import openpyxl
import time, re, csv, urllib.parse
options = Options()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-gpu')
driver = webdriver.Chrome('chromedriver', chrome_options=options)
  • 1
    You add "!apt-get update !apt-get install -y unzip xvfb libxi6 libgconf-2-4 !apt-get install default-jdk ", right? Sorry, but it still returns error code -6... why did you think it's effective? – Kohei Murakami Mar 11 '19 at 10:17
  • Ubuntu has changed its security policy. This explains the deadlock when trying to import Chrome. I've updated my question, it should work. – Tiffany Zucman Mar 11 '19 at 10:50
  • 1
    Thank you for your reply! I saw your edition, but it only differs in the part of commenting out unzip. It's still using chrome, and not working. Actually, downloading chrome wasn't problem , I guess. The last sentence make error. – Kohei Murakami Mar 11 '19 at 11:38