1

尝试使用此代码运行 selenium,但它一直说:

No module named 'options' found

代码:

import os
import sys
import subprocess
import pkg_resources
import selenium
from selenium import webdriver
#from selenium import options
import options
chromedriver = r"E:\Users\Yaseen Ahammed\Documents\Python Projects\chromedriver\chromedriver.exe"
#ChromeOptions = new ChromeOptions();
driver=webdriver.Chrome(chromedriver)
#binary_location
options.setBinary(r"E:\Users\Yaseen Ahammed\Documents\Python Projects\chromedriver\Chrome\Application\chrome.exe")

该错误是针对该行抛出的,import options但是没有它我会得到该options is not defined行的错误options.setBinary

4

3 回答 3

2

此错误消息...

No module named 'options' found

...表示您尝试导入的模块,即options

from selenium import options

未找到,因为它不是有效的模块。

setBinary()此外,您需要使用binary_location属性而不是使用方法。


解决方案

当您使用 ChromeDriver/Chrome 时,此问题有两种解决方案,如下所示:

  • 正如您已经使用的那样from selenium import webdriver,您可以:

    from selenium import webdriver
    
    options = webdriver.ChromeOptions()
    options.binary_location = r"E:\Users\Yaseen Ahammed\Documents\Python Projects\chromedriver\Chrome\Application\chrome.exe"    #chrome binary location specified here
    
  • 你也可以:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.binary_location = r"E:\Users\Yaseen Ahammed\Documents\Python Projects\chromedriver\Chrome\Application\chrome.exe"    #chrome binary location specified here
    

参考

您可以在以下位置找到一些相关的讨论:

于 2020-12-20T13:22:35.847 回答
1

我相信您可以导入特定网络驱动程序的选项,例如

from selenium.webdriver.chrome.options import Options或者from selenium.webdriver.firefox.options import Options

然后稍后你可以做options = Options(),然后选项的功能应该可用

于 2020-12-20T00:08:24.227 回答
1

我认为您需要为 chrome 驱动程序导入它。

from selenium.webdriver.chrome.options import Options

之后,你应该这样做

options = Options()

我相信你的问题会得到解决。

于 2020-12-20T08:20:16.613 回答