没有名为“选项”的模块异常

问题描述

尝试使用此代码运行 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 的错误。

解决方法

这个错误信息...

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
    

参考文献

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

,

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

error TS2769: No overload matches this call. Overload 1 of 5,'(observer?: NextObserver<[Edition | undefined,DocumentData,DocumentData]> | ErrorObserver<[Edition | undefined,DocumentData]> | CompletionObserver<...> | undefined): Subscription',gave the following error. Argument of type '([edition,sections,articles]: [Edition,Section[],Article[]]) => void' is not assignable to parameter of type 'NextObserver<[Edition | undefined,D ocumentData]> | CompletionObserver<...> | undefined'. Property 'complete' is missing in type '([edition,Article[]]) => void' but required in type 'CompletionObserver<[Edition | undefined,DocumentData]>'. Overload 2 of 5,'(next?: ((value: [Edition | undefined,DocumentData]) => void) | undefined,error?: ((error: any) => void) | undefined,complete?: (() => void) | undefined): Subscription',Article[]]) => void' is not assignable to parameter of type '(value: [Edition | undefined,DocumentData]) => void'. Types of parameters '__0' and 'value' are incompatible. Type '[Edition | undefined,DocumentData]' is not assignable to type '[Edition,Article[]]'. Type 'Edition | undefined' is not assignable to type 'Edition'. Type 'undefined' is not assignable to type 'Edition'. from selenium.webdriver.chrome.options import Options

然后您可以执行from selenium.webdriver.firefox.options import Options,然后选项的功能应该可用

,

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

from selenium.webdriver.chrome.options import Options

在那之后,你应该这样做

options = Options()

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...