PythonとSeleniumとWebDriverでドライバとブラウザのパス指定

 

import os
import time

from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.edge.options import Options as EdgeOptions
from selenium.webdriver.edge.service import Service as EdgeService
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.firefox.service import Service as FirefoxService


def main():

    print("PRESS ENTER KEY TO BOOT")
    input()

    driver = None

    try:
        # --------------------------------------------------------------
        # Firefox
        # --------------------------------------------------------------
        driver_location = str(os.getenv("LOCALAPPDATA")) + "\\geckodriver.exe"
        binary_location = str(os.getenv("PROGRAMFILES")) + "\\Mozilla Firefox\\firefox.exe"
        # binary_location = str(os.getenv("PROGRAMFILES")) + "\\Firefox Nightly\\firefox.exe"
        service = FirefoxService(executable_path=driver_location)
        options = FirefoxOptions()
        options.binary = binary_location
        # options.binary_location = binary_location
        driver = None
        driver = webdriver.Firefox(options=options, service=service)
        # driver = webdriver.Firefox()
        print(binary_location)
        print(driver_location)
        print(driver.session_id)
        print(driver.capabilities["platformName"])
        print(driver.capabilities["browserName"])
        print(driver.capabilities["browserVersion"])
        _ = "firefo" in driver.capabilities["browserName"] and print(driver.capabilities["moz:geckodriverVersion"])
        _ = "msedge" in driver.capabilities["browserName"] and print(driver.capabilities[driver.capabilities["browserName"]]["msedgedriverVersion"])
        _ = "chrome" in driver.capabilities["browserName"] and print(driver.capabilities[driver.capabilities["browserName"]]["chromedriverVersion"])
        driver.get("https://www.selenium.dev/ja/")
        time.sleep(1)
    except Exception as e:
        print(e)
    finally:
        driver = driver.quit() if driver is not None else None

    try:
        # --------------------------------------------------------------
        # Edge
        # --------------------------------------------------------------
        driver_location = str(os.getenv("LOCALAPPDATA")) + "\\msedgedriver.exe"
        binary_location = str(os.getenv("PROGRAMFILES(X86)")) + "\\Microsoft\\Edge\\Application\\msedge.exe"
        # binary_location = str(os.getenv("LOCALAPPDATA")) +    "\\Microsoft\\Edge SXS\\Application\\msedge.exe"
        service = EdgeService(executable_path=driver_location)
        options = EdgeOptions()
        options.binary_location = binary_location
        driver = None
        driver = webdriver.Edge(options=options, service=service)
        # driver = webdriver.Edge()
        print(binary_location)
        print(driver_location)
        print(driver.session_id)
        print(driver.capabilities["platformName"])
        print(driver.capabilities["browserName"])
        print(driver.capabilities["browserVersion"])
        _ = "firefo" in driver.capabilities["browserName"] and print(driver.capabilities["moz:geckodriverVersion"])
        _ = "msedge" in driver.capabilities["browserName"] and print(driver.capabilities[driver.capabilities["browserName"]]["msedgedriverVersion"])
        _ = "chrome" in driver.capabilities["browserName"] and print(driver.capabilities[driver.capabilities["browserName"]]["chromedriverVersion"])
        driver.get("https://www.selenium.dev/ja/")
        time.sleep(1)
    except Exception as e:
        print(e)
    finally:
        driver = driver.quit() if driver is not None else None

    try:
        # --------------------------------------------------------------
        # Chrome
        # --------------------------------------------------------------
        driver_location = str(os.getenv("LOCALAPPDATA")) + "\\chromedriver.exe"
        binary_location = str(os.getenv("PROGRAMFILES(X86)")) + "\\Google\\Chrome\\Application\\chrome.exe"
        # binary_location = str(os.getenv("LOCALAPPDATA")) +    "\\Google\\Chrome SxS\\Application\\chrome.exe"
        service = ChromeService(executable_path=driver_location)
        options = ChromeOptions()
        options.binary_location = binary_location
        driver = None
        driver = webdriver.Chrome(options=options, service=service)
        # driver = webdriver.Chrome()
        print(binary_location)
        print(driver_location)
        print(driver.session_id)
        print(driver.capabilities["platformName"])
        print(driver.capabilities["browserName"])
        print(driver.capabilities["browserVersion"])
        _ = "firefo" in driver.capabilities["browserName"] and print(driver.capabilities["moz:geckodriverVersion"])
        _ = "msedge" in driver.capabilities["browserName"] and print(driver.capabilities[driver.capabilities["browserName"]]["msedgedriverVersion"])
        _ = "chrome" in driver.capabilities["browserName"] and print(driver.capabilities[driver.capabilities["browserName"]]["chromedriverVersion"])
        driver.get("https://www.selenium.dev/ja/")
        time.sleep(1)
    except Exception as e:
        print(e)
    finally:
        driver = driver.quit() if driver is not None else None

    print()
    print("PRESS ENTER KEY TO EXIT")
    input()


if __name__ == "__main__":
    main()

Link1

Selenium ( ja )
https://www.selenium.dev/ja/documentation/
https://www.selenium.dev/ja/documentation/webdriver/
https://www.selenium.dev/ja/documentation/webdriver/drivers/options/
https://www.selenium.dev/ja/documentation/webdriver/getting_started/install_library/
https://www.selenium.dev/ja/documentation/webdriver/getting_started/install_drivers/
https://www.selenium.dev/ja/documentation/webdriver/getting_started/first_script/
https://www.selenium.dev/ja/documentation/webdriver/getting_started/upgrade_to_selenium_4/

Link2

Selenium
https://www.selenium.dev/documentation/
https://www.selenium.dev/documentation/webdriver/
https://www.selenium.dev/documentation/webdriver/drivers/options/
https://www.selenium.dev/documentation/webdriver/getting_started/install_library/
https://www.selenium.dev/documentation/webdriver/getting_started/install_drivers/
https://www.selenium.dev/documentation/webdriver/getting_started/first_script/
https://www.selenium.dev/documentation/webdriver/getting_started/upgrade_to_selenium_4/

Link3

Selenium Documentation
https://www.selenium.dev/selenium/docs/api/py/
https://www.selenium.dev/selenium/docs/api/rb/
https://www.selenium.dev/selenium/docs/api/java/
https://www.selenium.dev/selenium/docs/api/javascript/
https://www.selenium.dev/selenium/docs/api/dotnet/

Link4

Selenium Documentation API ( python )
https://www.selenium.dev/selenium/docs/api/py/api.html
https://www.selenium.dev/selenium/docs/api/py/webdriver/selenium.webdriver.common.options.html
https://www.selenium.dev/selenium/docs/api/py/webdriver_remote/selenium.webdriver.remote.webdriver.html
https://www.selenium.dev/selenium/docs/api/py/webdriver_chromium/selenium.webdriver.chromium.webdriver.html
https://www.selenium.dev/selenium/docs/api/py/webdriver_chromium/selenium.webdriver.chromium.options.html
https://www.selenium.dev/selenium/docs/api/py/webdriver_chromium/selenium.webdriver.chromium.service.html
https://www.selenium.dev/selenium/docs/api/py/webdriver_chrome/selenium.webdriver.chrome.webdriver.html
https://www.selenium.dev/selenium/docs/api/py/webdriver_chrome/selenium.webdriver.chrome.options.html
https://www.selenium.dev/selenium/docs/api/py/webdriver_chrome/selenium.webdriver.chrome.service.html
https://www.selenium.dev/selenium/docs/api/py/webdriver_edge/selenium.webdriver.edge.webdriver.html
https://www.selenium.dev/selenium/docs/api/py/webdriver_edge/selenium.webdriver.edge.options.html
https://www.selenium.dev/selenium/docs/api/py/webdriver_edge/selenium.webdriver.edge.service.html
https://www.selenium.dev/selenium/docs/api/py/webdriver_firefox/selenium.webdriver.firefox.webdriver.html
https://www.selenium.dev/selenium/docs/api/py/webdriver_firefox/selenium.webdriver.firefox.options.html
https://www.selenium.dev/selenium/docs/api/py/webdriver_firefox/selenium.webdriver.firefox.service.html

Link5

W3C Working Draft WebDriver
https://www.w3.org/TR/webdriver/
https://www.w3.org/TR/webdriver/#capabilities

Link6

MDN Web Docs WebDriver
https://developer.mozilla.org/en-US/docs/Web/WebDriver
https://developer.mozilla.org/en-US/docs/Web/WebDriver/Capabilities
https://developer.mozilla.org/en-US/docs/Web/WebDriver/Capabilities/firefoxOptions

Link7

Python
https://www.python.org/
https://www.python.org/downloads/

Selenium GitHub
https://github.com/SeleniumHQ
https://github.com/SeleniumHQ/selenium
https://github.com/SeleniumHQ/selenium/releases
https://github.com/SeleniumHQ/selenium/tree/trunk/py/selenium/webdriver
https://github.com/SeleniumHQ/selenium/blob/trunk/py/selenium/webdriver/init.py

geckodriver GitHub
https://github.com/mozilla
https://github.com/mozilla/geckodriver
https://github.com/mozilla/geckodriver/releases

ChromeDriver
https://chromedriver.chromium.org/
https://chromedriver.chromium.org/downloads
https://chromedriver.chromium.org/chromedriver-canary

Microsoft Edge WebDriver ( msedgedriver )
https://developer.microsoft.com/ja-jp/microsoft-edge/tools/webdriver/