[Python] Using Selenium via Python in JupyterLabs

Discussão em 'Python' iniciado por Stack, Fevereiro 11, 2025.

  1. Stack

    Stack Membro Participativo

    Summary of what I am trying to achieve: Use the selenium package in python within a JupyterLabs (cloud) environment.

    Suspected Issue: I suspect the issue/ (or complication) is resulting from the JupyterLabs environment.

    Why do I have to use JupyterLabs? My machine only allow me to run python via JupyterLabs

    Below explains the code I tried. I feel like I am close but just overlooking something. Really appreciate any guidance. Thanks in advance!

    I am using a machine which runs Python on JupyterLabs

    pip install selenium
    pip install webdriver_manager

    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.chrome.service import Options
    from webdriver_manager.chrome import ChromeDriverManager

    service = Service(executable_path=ChromeDriverManager().install())
    driver = webdriver.Chrome(service=service)
    driver.get("https://quotes.toscrape.com")


    I receive the error: "cannot find Chrome binary"

    I believe because this is JupyterLabs, it cannot read Chrome from my local Windows computer. So I downloaded the latest Win64 Chrome and drag and drop "chrome.exe" into my JupyterLabs file panel.

    First, to ensure I am referencing the Chrome correctly, I checked the path.

    import os
    os.getcwd()


    It returned: '/x1/jon'

    Next, I added these 2 lines of code to the codes above:

    options=Options()
    options.add_argument('--headless')
    options.binary_location = '/x1/jon/chrome.exe'


    And, I updated:

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


    The final code is:

    options=Options()
    options.add_argument('--headless')
    options.binary_location = '/x1/jon/chrome.exe'

    service = Service(executable_path=ChromeDriverManager().install())
    driver = webdriver.Chrome(service=service, options=options)
    driver.get("https://quotes.toscrape.com")


    Now, I get the error: "Chrome failed to start: exited abnormally"

    Would really appreciate any help or pointers on this. Many many thanks!

    The first step was to get ChromeDriver running, so I used

    service = Service(executable_path=ChromeDriverManager().install())


    Next, since it is JupyterLabs, I think it cannot read my Chrome. So I downloaded a Win64 Chrome.

    I dragged and drop the chrome.exe into JupyterLabs and reference it.

    However, I am still running into an error.

    Continue reading...

Compartilhe esta Página