如何在导入ChromeDriver时避免文件路径错误?

huangapple go评论63阅读模式
英文:

How do I avoid a file path error while importing ChromeDriver?

问题

I have been trying to work on my first web-scraping project for which I am using Selenium. However, I seem to be running into some issues with importing the ChromeDriver. I am using Selenium 3.0.0 and am working on Chrome.

webdriver_service = Service(ChromeDriverManager().install())
chrome_options = Options()
chrome_options.add_argument("--headless") # Ensure GUI is off
chrome_options.add_argument("--no-sandbox")

# Silent download of drivers
logging.getLogger('WDM').setLevel(logging.NOTSET)
os.environ['WDM_LOG'] = 'False'
driver = webdriver.Chrome(executable_path='/Users/MyUsername/Downloads/chromedriver.exe')

I keep getting the following message: 'chromedriver.exe' executable needs to be in PATH.

Let me know if there's some issue with the file path I am using as I think that is where the issue is coming from.

英文:

I have been trying to work on my first web-scraping project for which I am using Selenium. However, I seem to be running into some issues with importing the ChromeDriver. I am using Selenium 3.0.0 and am working on Chrome.

webdriver_service = Service(ChromeDriverManager().install())
chrome_options = Options()
chrome_options.add_argument("--headless") # Ensure GUI is off
chrome_options.add_argument("--no-sandbox")

# Silent download of drivers
logging.getLogger('WDM').setLevel(logging.NOTSET)
os.environ['WDM_LOG'] = 'False'
driver = webdriver.Chrome(executable_path='/Users/MyUsername/Downloads/chromedriver.exe')

I keep getting the following message: 'chromedriver.exe' executable needs to be in PATH.

Let me know if there's some issue with the file path I am using as I think that is where the issue is coming from.

答案1

得分: 1

这应该尽可能通过操作系统来完成,而不是通过代码,因为需要为每个项目都这样做,但也可以通过代码来设置环境变量。

import os
from selenium import webdriver
from selenium.webdriver.chrome.service import Service

# 设置所需的环境变量
os.environ["CHROME_DRIVER_PATH"] = "Z:\\port\\driver\\chromedriver"

# 读取我们创建的变量
DRIVER_PATH = os.environ.get('CHROME_DRIVER_PATH')
s = Service(DRIVER_PATH)

我从这里找到了答案。

英文:

This should be done through the OS when possible and not through code as it needs to be done for every project this way, but it is possible to set environment variables through code.

import os
from selenium import webdriver
from selenium.webdriver.chrome.service import Service

# Setting said environment variable
os.environ["CHROME_DRIVER_PATH"] = "Z:\\port\\driver\\chromedriver"

# Reading the variable we created
DRIVER_PATH = os.environ.get('CHROME_DRIVER_PATH')
s=Service(DRIVER_PATH)

I got my answer from here.

答案2

得分: 0

根据答案 提到的,你应该指定chromedriver路径如下:

driver = webdriver.Chrome('/Users/MyUsername/Downloads/chromedriver.exe')

executable_path= 参数代表chrome.exe,即谷歌浏览器。

英文:

As answer tells, you should specify the chromedriver-path as following:

driver = webdriver.Chrome('/Users/MyUsername/Downloads/chromedriver.exe')

The executable_path= argument stands for chrome.exe, meaning google-chrome browser.

答案3

得分: 0

以下是翻译好的部分:

"I'm going to be honest and say the path looks good to me. I even tried to run it in Visual Studio and it worked perfectly, so I have no idea why it isn't working for you. That being said I do have a way you can fix it. You can just not use executable_path. You already imported webdriver whose sole purpose is to handle the webdriver for selenium for you.

See here.

You don't even have to change much.

#pip install webdriver-manager, pip install selenium==3.0.0

import os
import logging
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager


chrome_options = Options()
chrome_options.add_argument("--headless") # Ensure GUI is off
chrome_options.add_argument("--no-sandbox")

# Silent download of drivers
logging.getLogger('WDM').setLevel(logging.NOTSET)
os.environ['WDM_LOG'] = 'False'
driver = webdriver.Chrome(ChromeDriverManager().install())
#driver = webdriver.Chrome(executable_path='/Users/MyUsername/Downloads/chromedriver.exe')


driver.get('https://www.google.com/')

Hope this helps.

"

英文:

I'm going to be honest and say the path looks good to me. I even tried to run it in Visual Studio and it worked perfectly, so I have no idea why it isn't working for you. That being said I do have a way you can fix it. You can just not use executable_path. You already imported webdriver whose sole purpose is to handle the webdriver for selenium for you.

See here.

You don't even have to change much.

#pip install webdriver-manager, pip install selenium==3.0.0

import os
import logging
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager


chrome_options = Options()
chrome_options.add_argument("--headless") # Ensure GUI is off
chrome_options.add_argument("--no-sandbox")

# Silent download of drivers
logging.getLogger('WDM').setLevel(logging.NOTSET)
os.environ['WDM_LOG'] = 'False'
driver = webdriver.Chrome(ChromeDriverManager().install())
#driver = webdriver.Chrome(executable_path='/Users/MyUsername/Downloads/chromedriver.exe')


driver.get('https://www.google.com/')

Hope this helps.

答案4

得分: 0

你需要注意以下几点:

  • 由于你正在使用 Selenium 3.0.0,你不需要创建任何 Service() 对象。所以你可以移除这一行:

    webdriver_service = Service(ChromeDriverManager().install())
    
  • 如果你不打算使用 ChromeDriverManager(),你可以移除以下几行:

    # 静默下载驱动程序
    logging.getLogger('WDM').setLevel(logging.NOTSET)
    os.environ['WDM_LOG'] = 'False'
    
  • 除非你在 headless 模式下执行测试,否则你不需要 --headless 参数。所以你可以移除这一行:

    chrome_options.add_argument("--headless") # 确保图形界面关闭
    
  • 除非你以管理员或 root 用户身份执行测试,你不需要 --no-sandbox 参数。所以你可以移除这一行:

    chrome_options.add_argument("--no-sandbox")
    

现在你可以从 ChromeDriver 页面下载匹配的 ChromeDriver 版本,解压缩/解包它,然后使用以下代码:

from selenium import webdriver

driver = webdriver.Chrome(executable_path='/path/to/chromedriver') # 对于 Windows 操作系统,请使用 chromedriver.exe
driver.get('https://www.google.com/')
英文:

You have to take care of a couple of things:

  • As you are using Selenium 3.0.0 you don't have to create any Service() object. So you can remove the line:

    webdriver_service = Service(ChromeDriverManager().install())
    
  • Incase you don't intend to use ChromeDriverManager() you can remove the following lines:

    # Silent download of drivers
    logging.getLogger('WDM').setLevel(logging.NOTSET)
    os.environ['WDM_LOG'] = 'False'
    
  • Unless you are executing your test in headless mode you won't require the argument --headless. So you can remove the line:

    chrome_options.add_argument("--headless") # Ensure GUI is off
    
  • Unless you are executing your test as a root/administrator you won't nee the argument --no-sandbox. So you can remove the line:

    chrome_options.add_argument("--no-sandbox")
    

Now you can download the matching ChromeDriver version from ChromeDriver page, unzip/untar it and use the following lines of code:

from selenium import webdriver

driver = webdriver.Chrome(executable_path='/path/to/chromedriver') # for windows OS use chromedriver.exe
driver.get('https://www.google.com/')

huangapple
  • 本文由 发表于 2023年2月9日 01:40:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75389719.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定