How to implement "Selenium" using python

I have installed “selenium” using the terminal, but I am unable to run the below code successfully. I am getting the error: WebDriverException

I am using .venv kernel
If it is an issue with the ChromeDriver then please give me the steps to solve this problem.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time

driver = webdriver.Chrome()
driver.get(“http://www.python.org”)
assert “Python” in driver.title
elem = driver.find_element(By.NAME, “q”)
elem.clear()
elem.send_keys(“pycon”)
elem.send_keys(Keys.RETURN)
assert “No results found.” not in driver.page_source
time.sleep(6)
driver.close()

Sadly I could not make Chrome WebDriver work either, but using Firefox WebDriver works, I wrote this flake.nix file because stable-24.05 and unstable channels are outdated making firefox and geckodriver be incompatibles [ at least that happened in my workspace ].

{
  description = "Selenium Environment";

  inputs = {
    # using nixos-unstable because geckodriver is outdated in nixos-24.05
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs = { self, nixpkgs, flake-utils }:
    flake-utils.lib.eachDefaultSystem (system:    
      let
        pkgs = import nixpkgs { inherit system; };
      in
      with pkgs;
      {
        devShells.default = mkShell {        
          buildInputs = [ firefox geckodriver ];

          # Somehow VSCode terminal pops up with "bash: __vsc_prompt_cmd_original: command not found"
          shellHook = ''
            unset PROMPT_COMMAND
          '';
        };
      }
    );
}

and respectively using the firefox driver in the Python code:

options = webdriver.FirefoxOptions()
# required for idx.dev because no user interface is available
options.add_argument('--headless')
driver = webdriver.Firefox(options=options)

Otherwise we will have to wait for some solution If you really want to use the Chrome Web Driver.

1 Like

Have you tried using chromium-headless?