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

2024-09-29 19:18:47 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试使用此代码运行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.setBinary行上得到options is not defined的错误


Tags: fromimportseleniumchromeexechromedriverusersdocuments
3条回答

我相信您可以为特定的webdriver导入选项,例如

from selenium.webdriver.chrome.options import Optionsfrom selenium.webdriver.firefox.options import Options

然后,您可以稍后执行options = Options(),然后选项的函数应该可用

我想你需要为chrome驱动程序导入这个

from selenium.webdriver.chrome.options import Options

在那之后,你应该这样做

options = Options()

我相信你的问题会解决的

此错误消息

No module named 'options' found

…表示您尝试导入的模块,即options,如中所示:

from selenium import options

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

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


解决方案

当您使用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
    

参考资料

您可以在以下内容中找到一些相关讨论:

相关问题 更多 >

    热门问题