How can I get selenium chrome driver using python running in Docker
Asked Answered
A

2

0

I'm trying to get selenium chrome driver running inside Docker. Why am I getting this error message? Is there something wrong in the Dockerfile code? I'm trying this on Windows 10 + m1 mac operating systems and neither one is working.



INFO:WDM:


====== WebDriver manager ======

INFO:WDM:====== WebDriver manager ======

Current google-chrome version is 101.0.4951

INFO:WDM:Current google-chrome version is 101.0.4951

Get LATEST chromedriver version for 101.0.4951 google-chrome

INFO:WDM:Get LATEST chromedriver version for 101.0.4951 google-chrome

There is no [linux64] chromedriver for browser 101.0.4951 in cache

INFO:WDM:There is no [linux64] chromedriver for browser 101.0.4951 in cache

Trying to download new driver from https://chromedriver.storage.googleapis.com/101.0.4951.41/chromedriver_linux64.zip

INFO:WDM: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]

INFO:WDM:Driver has been saved in cache [/root/.wdm/drivers/chromedriver/linux64/101.0.4951.41]

Dockerfile code:

FROM python:3.9 as py
LABEL maintainer="####"

# Adding trusting keys to apt for repositories
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
# Adding Google Chrome to the repositories
RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
# Updating apt to see and install Google Chrome
RUN apt-get -y update
# Magic happens
RUN apt-get install -y google-chrome-stable

# Installing Unzip
RUN apt-get install -yqq unzip
# Download the Chrome Driver
RUN wget -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zip 

# Unzip the Chrome Driver into /usr/local/bin directory
RUN unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/

COPY . /code_review
WORKDIR /code_review
RUN pip install -r requirements.txt
CMD python server.py

Here is the code to my Python script, i'm using selenium to automate a process in SonarCloud:

# importing necessary libraries
import github3
from github3 import login
from http.server import executable
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time
from sonarqube import SonarCloudClient
from sonarqube import SonarQubeClient
from config import GITHUB_API_KEY, SONARCLOUD_API_KEY, GITHUB_USERNAME, GITHUB_PASSWORD
sonar = SonarCloudClient(sonarcloud_url="https://sonarcloud.io", token=SONARCLOUD_API_KEY)

# Login using a personal access token
github = github3.login(token=GITHUB_API_KEY)

# forking all public repositories for given user
def ForkRepos(username):
    for repository in github.repositories_by(username):
        repository.create_fork()
    time.sleep(5)


# Conducting code review in SonarCloud on all repositories 
def SonarAnalysis():
    options = webdriver.ChromeOptions()
    options.add_experimental_option("detach", True)
    options.add_argument('headless')
    driver = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=options)
    driver.get('https://sonarcloud.io/')
    githubclick = driver.find_element_by_xpath('//*[@id="gatsby-focus-wrapper"]/div/div/div[2]/div[1]/div/div/div/a[1]')
    githubclick.click()
    githubusername = driver.find_element_by_xpath('//*[@id="login_field"]')
    githubusername.send_keys(GITHUB_USERNAME)
    githubpassword = driver.find_element_by_xpath('//*[@id="password"]')
    githubpassword.send_keys(GITHUB_PASSWORD)
    githubsigninclick = driver.find_element_by_xpath('//*[@id="login"]/div[3]/form/div/input[12]')
    githubsigninclick.click()
    time.sleep(5)
    plussign = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div/div[1]/div/nav/div/div/ul[1]/li[3]/button')
    plussign.click()
    analyzeprojects = driver.find_element_by_xpath('//*[@id="global-navigation"]/div/div/ul[1]/li[3]/div/ul/li[1]/a')
    analyzeprojects.click()
    time.sleep(5)
    selectallrepos = driver.find_element_by_xpath('//*[@id="container"]/div/div/div[2]/div[1]/div/div[1]/a/i')
    selectallrepos.click()
    time.sleep(5)
    reposetup = driver.find_element_by_xpath('//*[@id="container"]/div/div/div[2]/div[2]/div/form/div[2]/div[2]/button')
    reposetup.click()
    time.sleep(300)

def GetCodeReview():
    analyzed_repos = list(sonar.favorites.search_favorites())
    analyzed_repo_list = []
    for i in range(len(analyzed_repos)):
        repos = analyzed_repos[i]
        analyzed_repo_list.append(repos["key"])

    repo_review = {}
    for repo_name in analyzed_repo_list:
        analysis = list(sonar.issues.search_issues(componentKeys=repo_name))
        a = {}
        for i in range(len(analysis)):
            item = analysis[i]
            if item["author"] in a:
                a[item["author"]][item["type"]] = a[item["author"]][item["type"]] + 1                
            else:
                a[item["author"]] = {"BUG":0, "CODE_SMELL":0, "VULNERABILITY":0}
                a[item["author"]][item["type"]] = 1      
        repo_review[repo_name] = a
    return repo_review


def getreview(username):
    ForkRepos(username)
    SonarAnalysis()
    analyzed_repos = list(sonar.favorites.search_favorites())
    analyzed_repo_list = []
    for i in range(len(analyzed_repos)):
        repos = analyzed_repos[i]
        analyzed_repo_list.append(repos["key"])

    repo_review = {}
    for repo_name in analyzed_repo_list:
        analysis = list(sonar.issues.search_issues(componentKeys=repo_name))
        a = {}
        for i in range(len(analysis)):
            item = analysis[i]
            if item["author"] in a:
                a[item["author"]][item["type"]] = a[item["author"]][item["type"]] + 1                
            else:
                a[item["author"]] = {"BUG":0, "CODE_SMELL":0, "VULNERABILITY":0}
                a[item["author"]][item["type"]] = 1      
        repo_review[repo_name] = a
    return repo_review
Afterclap answered 16/5, 2022 at 5:59 Comment(0)
G
1

it's better to post your codes of how you import/install/use selenium.webdriver and webdriver_manager in server.py and specify which OS and machine you're running your codes.

Glint answered 16/5, 2022 at 6:47 Comment(9)
Thanks! i've updated the question to include my python script.Afterclap
@JackJones for this silent situation, I suggest you enable more verbose logging since by default ChromeDriver logs only warnings/errors to stderr. Try driver = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=options, service_args=["--verbose", "--log-path=YOUR LINUX PATH"]) and then see if you can find out something weird in the log or just paste the log here.Glint
Thanks! could you please help explain what the log-path is? How can I find my linux path?Afterclap
something like /tmp/chromedriver.log should work. then check out that fileGlint
I tried this but nothing seemed to change the output from beforeAfterclap
got it. if so, do you want to try to revert to the old version of Chrome web driver such as 100.0.4896.60 or 99.0.4844.51 by manually downloading its Linux executable file from chromedriver.storage.googleapis.com/index.html and to your local and then use the way of Hard-Coded Location instead of a web driver manager (selenium.dev/documentation/webdriver/getting_started/…) ?Glint
@JackJones were you able to run python selenium, chrome and chromedriver in Docker on Mac M1 after all?Artie
hey @Artie this is a bit of a late reply, but no I wasn't able to. I had switched to running it on Windows.Afterclap
@Afterclap got it. sorry I also missed your comment. thanks for reverting. have a great day!Artie
F
0

I was able to make it work by only downloading chrome and then using webdriver_manager to get the correct chromedriver. Here are my Dockerfile and get_driver method:

FROM --platform=linux/amd64 python:3.12

WORKDIR /app

RUN mkdir __logger

# install google chrome
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list
RUN apt-get update && apt-get install -y google-chrome-stable

# set display port to avoid crash
ENV DISPLAY=:99

RUN pip install --upgrade pip

COPY . /app

RUN pip install -r requirements.txt

RUN google-chrome --version

CMD ["python", "run.py"]
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

def get_driver():
    service = Service(ChromeDriverManager().install())

    options = webdriver.ChromeOptions()
    options.add_argument("--disable-extensions")

    options.add_argument("--headless")
    options.add_argument("--no-sandbox")
    options.add_argument("--disable-dev-shm-usage")
    options.add_argument("--enable-gpu")
    
    prefs = {
        "profile.default_content_settings": {"images": 2}
    }
    options.add_experimental_option("prefs", prefs)

    driver = webdriver.Chrome(
        service=service, options=options)

    return driver
Flaggy answered 27/6 at 13:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.