如何解决没有名为“选项”的模块异常
尝试使用此代码运行 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
参考文献
您可以在以下位置找到一些相关讨论:
- Set chrome browser binary through chromedriver in Python
- AttributeError: 'Options' object has no attribute 'binary' error invoking Headless Firefox using GeckoDriver through Selenium
我相信您可以为特定的网络驱动程序导入选项,例如
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()
我相信你的问题会得到解决。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。