【问题标题】:No module named 'options' exception没有名为“选项”的模块异常
【发布时间】:2020-12-19 23:46:14
【问题描述】:

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

【问题讨论】:

    标签: python selenium selenium-chromedriver options chrome-options


    【解决方案1】:

    此错误消息...

    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
      

    参考文献

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

    【讨论】:

    • 感谢您,我检查了 python selenium 文档,但在当前版本中找不到任何相关信息,并且知道每个版本更改都会更改可以识别的代码行,很难知道在新版本中什么仍然有效,什么无效,这就是我最终打开这个线程的原因。有没有包含python中selenium包可以识别的每个参数/代码行的官方文档?
    • 好的,谢谢!当我尝试这样做时,我现在收到“options.setBinary”行的“Message='Options' object has no attribute 'setBinary'”的错误。
    • @mya205 查看更新的答案并让我知道状态。
    • 谢谢!我想我已经用“binary_location”解决了这个问题,而不是“setBinary”,这是我看到很多人提到的。这种差异是由于版本差异还是其他原因?我注意到其他人可能在一两年前提到的很多时间代码似乎不起作用,所以我假设这只是版本差异。由于这些版本更改等,它很快就会变得非常混乱,所以感谢您的帮助,它真的很有用。
    【解决方案2】:

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

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

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

    【讨论】:

    • 当我尝试这样做时,我现在收到“options.setBinary”行的“Message='Options' 对象没有属性 'setBinary'”的错误。
    【解决方案3】:

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

    from selenium.webdriver.chrome.options import Options
    

    之后,你应该这样做

    options = Options()
    

    相信你的问题会解决的。

    【讨论】:

    • 当我尝试这样做时,我现在收到“options.setBinary”行的“Message='Options' object has no attribute 'setBinary'”的错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-04
    • 2021-09-12
    • 2022-12-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-14
    相关资源
    最近更新 更多