Python没有这样的文件或目录getAttribute.js

Python没有这样的文件或目录getAttribute.js,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,大约一周前,我有一个Python selenium程序正在运行。现在,selenium导入失败: from selenium import webdriver 空闲输出: Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>

大约一周前,我有一个Python selenium程序正在运行。现在,selenium导入失败:

from selenium import webdriver
空闲输出:

Python 3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> 
 RESTART: C:\Users\Mike\AppData\Local\Programs\Python\Python36-32\testpack.py 
Traceback (most recent call last):
  File "C:\Users\Mike\AppData\Local\Programs\Python\Python36-32\testpack.py", line 1, in <module>
    from selenium import webdriver
  File "C:\Users\Mike\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\__init__.py", line 18, in <module>
    from .firefox.webdriver import WebDriver as Firefox  # noqa
  File "C:\Users\Mike\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 35, in <module>
    from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
  File "C:\Users\Mike\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 26, in <module>
    from .webelement import WebElement
  File "C:\Users\Mike\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 43, in <module>
    getAttribute_js = pkgutil.get_data(_pkg, 'getAttribute.js').decode('utf8')
  File "C:\Users\Mike\AppData\Local\Programs\Python\Python36-32\lib\pkgutil.py", line 634, in get_data
    return loader.get_data(resource_name)
  File "<frozen importlib._bootstrap_external>", line 832, in get_data
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Mike\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages\\selenium\\webdriver\\remote\\getAttribute.js'
>>> 
win32上的Python 3.6.3(v3.6.3:2c5fed8,2017年10月3日,17:26:49)[MSC v.1900 32位(英特尔)] 有关详细信息,请键入“copyright”、“credits”或“license()”。 >>> 重新启动:C:\Users\Mike\AppData\Local\Programs\Python\Python36-32\testpack.py 回溯(最近一次呼叫最后一次): 文件“C:\Users\Mike\AppData\Local\Programs\Python\Python36-32\testpack.py”,第1行,在 从selenium导入webdriver 文件“C:\Users\Mike\AppData\Local\Programs\Python\Python36-32\lib\site packages\selenium\webdriver\uuuu init\uuuu.py”,第18行,在 从.firefox.webdriver将webdriver导入为firefox#noqa 文件“C:\Users\Mike\AppData\Local\Programs\Python36-32\lib\site packages\selenium\webdriver\firefox\webdriver.py”,第35行,在 从selenium.webdriver.remote.webdriver将webdriver作为RemoteWebDriver导入 文件“C:\Users\Mike\AppData\Local\Programs\Python36-32\lib\site packages\selenium\webdriver\remote\webdriver.py”,第26行,在 from.webelement导入webelement 文件“C:\Users\Mike\AppData\Local\Programs\Python36-32\lib\site packages\selenium\webdriver\remote\webelement.py”,第43行,在 getAttribute_js=pkgutil.get_数据(_pkg,'getAttribute.js')。解码('utf8') 文件“C:\Users\Mike\AppData\Local\Programs\Python\Python36-32\lib\pkgutil.py”,第634行,在get\U数据中 返回加载器。获取\u数据(资源\u名称) get_数据中第832行的文件“” FileNotFoundError:[Errno 2]没有这样的文件或目录:“C:\\Users\\Mike\\AppData\\Local\\Programs\\Python36-32\\lib\\site packages\\selenium\\webdriver\\remote\\getAttribute.js” >>> 当我尝试用pip重新安装软件包时,我被告知满足了要求

chromedriver.exe位于c:\Windows\system32中


非常感谢您的任何想法。

您的webdriver安装文件已损坏或丢失。重新安装/升级Web驱动程序

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Mike\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages\\selenium\\webdriver\\remote\\getAttribute.js'
试试这个:

pip install selenium --upgrade

您的webdriver安装文件已损坏或丢失。重新安装/升级Web驱动程序

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Mike\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages\\selenium\\webdriver\\remote\\getAttribute.js'
试试这个:

pip install selenium --upgrade

该错误确实为我们提供了一些关于发生了什么错误的提示,如下所示:

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Mike\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages\\selenium\\webdriver\\remote\\getAttribute.js'
from selenium import webdriver

driver = webdriver.Firefox(executable_path="C:\\path\\to\\geckodriver.exe")
driver.get('https://stackoverflow.com')
这本质上意味着getAttribute.js未在其预期位置找到

getAttribute.js
getAttribute.js
文件在内部处理错误和其他关键JavaScript函数,如下所示:

  • 元素不可选择
  • 元素不可见
  • 无效的cookie域
  • 无效元素坐标
  • 无效元素状态
  • 无效选择器
  • 无效选择器
  • parseInt()
  • indexOf()
  • toUpperCase()
  • execScript()
典型的
getAttribute.js
文件如下所示:

function(){return function(){var d=this;function f(a){return"string"==typeof a};function h(a,b){this.code=a;this.a=l[a]||m;this.message=b||"";a=this.a.replace(/((?:^|\s+)[a-z])/g,function(a){return a.toUpperCase().replace(/^[\s\xa0]+/g,"")});b=a.length-5;if(0>b||a.indexOf("Error",b)!=b)a+="Error";this.name=a;a=Error(this.message);a.name=this.name;this.stack=a.stack||""}
(function(){var a=Error;function b(){}b.prototype=a.prototype;h.b=a.prototype;h.prototype=new b;h.prototype.constructor=h;h.a=function(b,c,g){for(var e=Array(arguments.length-2),k=2;k<arguments.length;k++)e[k-2]=arguments[k];return a.prototype[c].apply(b,e)}})();var m="unknown error",l={15:"element not selectable",11:"element not visible"};l[31]=m;l[30]=m;l[24]="invalid cookie domain";l[29]="invalid element coordinates";l[12]="invalid element state";l[32]="invalid selector";l[51]="invalid selector";
l[52]="invalid selector";l[17]="javascript error";l[405]="unsupported operation";l[34]="move target out of bounds";l[27]="no such alert";l[7]="no such element";l[8]="no such frame";l[23]="no such window";l[28]="script timeout";l[33]="session not created";l[10]="stale element reference";l[21]="timeout";l[25]="unable to set cookie";l[26]="unexpected alert open";l[13]=m;l[9]="unknown command";h.prototype.toString=function(){return this.name+": "+this.message};var n;a:{var p=d.navigator;if(p){var q=p.userAgent;if(q){n=q;break a}}n=""}function r(a){return-1!=n.indexOf(a)};function t(a,b){for(var e=a.length,c=f(a)?a.split(""):a,g=0;g<e;g++)g in c&&b.call(void 0,c[g],g,a)};function v(){return r("iPhone")&&!r("iPod")&&!r("iPad")};function w(){return(r("Chrome")||r("CriOS"))&&!r("Edge")};var x=r("Opera"),y=r("Trident")||r("MSIE"),z=r("Edge"),A=r("Gecko")&&!(-1!=n.toLowerCase().indexOf("webkit")&&!r("Edge"))&&!(r("Trident")||r("MSIE"))&&!r("Edge"),aa=-1!=n.toLowerCase().indexOf("webkit")&&!r("Edge");function B(){var a=d.document;return a?a.documentMode:void 0}var C;
a:{var D="",E=function(){var a=n;if(A)return/rv\:([^\);]+)(\)|;)/.exec(a);if(z)return/Edge\/([\d\.]+)/.exec(a);if(y)return/\b(?:MSIE|rv)[: ]([^\);]+)(\)|;)/.exec(a);if(aa)return/WebKit\/(\S+)/.exec(a);if(x)return/(?:Version)[ \/]?(\S+)/.exec(a)}();E&&(D=E?E[1]:"");if(y){var F=B();if(null!=F&&F>parseFloat(D)){C=String(F);break a}}C=D}var G;var H=d.document;G=H&&y?B()||("CSS1Compat"==H.compatMode?parseInt(C,10):5):void 0;var ba=r("Firefox"),ca=v()||r("iPod"),da=r("iPad"),I=r("Android")&&!(w()||r("Firefox")||r("Opera")||r("Silk")),ea=w(),J=r("Safari")&&!(w()||r("Coast")||r("Opera")||r("Edge")||r("Silk")||r("Android"))&&!(v()||r("iPad")||r("iPod"));function K(a){return(a=a.exec(n))?a[1]:""}(function(){if(ba)return K(/Firefox\/([0-9.]+)/);if(y||z||x)return C;if(ea)return v()||r("iPad")||r("iPod")?K(/CriOS\/([0-9.]+)/):K(/Chrome\/([0-9.]+)/);if(J&&!(v()||r("iPad")||r("iPod")))return K(/Version\/([0-9.]+)/);if(ca||da){var a=/Version\/(\S+).*Mobile\/(\S+)/.exec(n);if(a)return a[1]+"."+a[2]}else if(I)return(a=K(/Android\s+([0-9.]+)/))?a:K(/Version\/([0-9.]+)/);return""})();var L,M=function(){if(!A)return!1;var a=d.Components;if(!a)return!1;try{if(!a.classes)return!1}catch(g){return!1}var b=a.classes,a=a.interfaces,e=b["@mozilla.org/xpcom/version-comparator;1"].getService(a.nsIVersionComparator),c=b["@mozilla.org/xre/app-info;1"].getService(a.nsIXULAppInfo).version;L=function(a){e.compare(c,""+a)};return!0}(),N=y&&!(8<=Number(G)),fa=y&&!(9<=Number(G));I&&M&&L(2.3);I&&M&&L(4);J&&M&&L(6);var ga={SCRIPT:1,STYLE:1,HEAD:1,IFRAME:1,OBJECT:1},O={IMG:" ",BR:"\n"};function P(a,b,e){if(!(a.nodeName in ga))if(3==a.nodeType)e?b.push(String(a.nodeValue).replace(/(\r\n|\r|\n)/g,"")):b.push(a.nodeValue);else if(a.nodeName in O)b.push(O[a.nodeName]);else for(a=a.firstChild;a;)P(a,b,e),a=a.nextSibling};function Q(a,b){b=b.toLowerCase();return"style"==b?ha(a.style.cssText):N&&"value"==b&&R(a,"INPUT")?a.value:fa&&!0===a[b]?String(a.getAttribute(b)):(a=a.getAttributeNode(b))&&a.specified?a.value:null}var ia=/[;]+(?=(?:(?:[^"]*"){2})*[^"]*$)(?=(?:(?:[^']*'){2})*[^']*$)(?=(?:[^()]*\([^()]*\))*[^()]*$)/;
function ha(a){var b=[];t(a.split(ia),function(a){var c=a.indexOf(":");0<c&&(a=[a.slice(0,c),a.slice(c+1)],2==a.length&&b.push(a[0].toLowerCase(),":",a[1],";"))});b=b.join("");return b=";"==b.charAt(b.length-1)?b:b+";"}function S(a,b){N&&"value"==b&&R(a,"OPTION")&&null===Q(a,"value")?(b=[],P(a,b,!1),a=b.join("")):a=a[b];return a}function R(a,b){b&&"string"!==typeof b&&(b=b.toString());return!!a&&1==a.nodeType&&(!b||a.tagName.toUpperCase()==b)}
function T(a){return R(a,"OPTION")?!0:R(a,"INPUT")?(a=a.type.toLowerCase(),"checkbox"==a||"radio"==a):!1};var ja={"class":"className",readonly:"readOnly"},U="async autofocus autoplay checked compact complete controls declare defaultchecked defaultselected defer disabled draggable ended formnovalidate hidden indeterminate iscontenteditable ismap itemscope loop multiple muted nohref noresize noshade novalidate nowrap open paused pubdate readonly required reversed scoped seamless seeking selected spellcheck truespeed willvalidate".split(" ");function V(a,b){var e=null,c=b.toLowerCase();if("style"==c)return(e=a.style)&&!f(e)&&(e=e.cssText),e;if(("selected"==c||"checked"==c)&&T(a)){if(!T(a))throw new h(15,"Element is not selectable");b="selected";e=a.type&&a.type.toLowerCase();if("checkbox"==e||"radio"==e)b="checked";return S(a,b)?"true":null}var g=R(a,"A");if(R(a,"IMG")&&"src"==c||g&&"href"==c)return(e=Q(a,c))&&(e=S(a,c)),e;if("spellcheck"==c){e=Q(a,c);if(null!==e){if("false"==e.toLowerCase())return"false";if("true"==e.toLowerCase())return"true"}return S(a,
c)+""}g=ja[b]||b;a:if(f(U))c=f(c)&&1==c.length?U.indexOf(c,0):-1;else{for(var u=0;u<U.length;u++)if(u in U&&U[u]===c){c=u;break a}c=-1}if(0<=c)return(e=null!==Q(a,b)||S(a,g))?"true":null;try{var k=S(a,g)}catch(ka){}(c=null==k)||(c=typeof k,c="object"==c&&null!=k||"function"==c);c?e=Q(a,b):e=k;return null!=e?e.toString():null}var W=["_"],X=d;W[0]in X||!X.execScript||X.execScript("var "+W[0]);
for(var Y;W.length&&(Y=W.shift());){var Z;if(Z=!W.length)Z=void 0!==V;Z?X[Y]=V:X[Y]&&X[Y]!==Object.prototype[Y]?X=X[Y]:X=X[Y]={}};; return this._.apply(null,arguments);}.apply({navigator:typeof window!='undefined'?window.navigator:null,document:typeof window!='undefined'?window.document:null}, arguments);}
  • 确保pip正常工作

    C:\Users\username>pip
    
    Usage:
      pip <command> [options]
    
    Commands:
      install                     Install packages.
      download                    Download packages.
      uninstall                   Uninstall packages.
      freeze                      Output installed packages in requirements format.
      list                        List installed packages.
      show                        Show information about installed packages.
      check                       Verify installed packages have compatible dependencies.
      search                      Search PyPI for packages.
      wheel                       Build wheels from your requirements.
      hash                        Compute hashes of package archives.
      completion                  A helper command used for command completion.
      help                        Show help for commands.
    
    General Options:
      -h, --help                  Show help.
      --isolated                  Run pip in an isolated mode, ignoring environment variables and user configuration.
      -v, --verbose               Give more output. Option is additive, and can be used up to 3 times.
      -V, --version               Show version and exit.
      -q, --quiet                 Give less output. Option is additive, and can be used up to 3 times (corresponding to WARNING, ERROR, and CRITICAL logging levels).
      --log <path>                Path to a verbose appending log.
      --proxy <proxy>             Specify a proxy in the form [user:passwd@]proxy.server:port.
      --retries <retries>         Maximum number of retries each connection should attempt (default 5 times).
      --timeout <sec>             Set the socket timeout (default 15 seconds).
      --exists-action <action>    Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort.
      --trusted-host <hostname>   Mark this host as trusted, even though it does not have valid or any HTTPS.
      --cert <path>               Path to alternate CA bundle.
      --client-cert <path>        Path to SSL client certificate, a single file containing the private key and the certificate in PEM format.
      --cache-dir <dir>           Store the cache data in <dir>.
      --no-cache-dir              Disable the cache.
      --disable-pip-version-check
                      Don't periodically check PyPI to determine
                      whether a new version of pip is available for
                  download. Implied with --no-index.
    
  • 通过pip安装最新的Selenium

    C:\Users\username>pip uninstall selenium
    Uninstalling selenium-3.4.2:
      c:\python\lib\site-packages\selenium-3.4.2.dist-info\description.rst
      c:\python\lib\site-packages\selenium-3.4.2.dist-info\installer
      c:\python\lib\site-packages\selenium-3.4.2.dist-info\metadata
      c:\python\lib\site-packages\selenium-3.4.2.dist-info\metadata.json
      c:\python\lib\site-packages\selenium-3.4.2.dist-info\record
      c:\python\lib\site-packages\selenium-3.4.2.dist-info\top_level.txt
      c:\python\lib\site-packages\selenium-3.4.2.dist-info\wheel
      c:\python\lib\site-packages\selenium\__init__.py
      c:\python\lib\site-packages\selenium\__pycache__\__init__.cpython-36.pyc
      c:\python\lib\site-packages\selenium\common\__init__.py
      .....
      .....
      .....  
      c:\python\lib\site-packages\selenium\webdriver\support\events.py
      c:\python\lib\site-packages\selenium\webdriver\support\expected_conditions.py
      c:\python\lib\site-packages\selenium\webdriver\support\select.py
      c:\python\lib\site-packages\selenium\webdriver\support\ui.py
      c:\python\lib\site-packages\selenium\webdriver\support\wait.py
      c:\python\selenium\webdriver\firefox\amd64\x_ignore_nofocus.so
      c:\python\selenium\webdriver\firefox\x86\x_ignore_nofocus.so
      c:\python\selenium\webdriver\remote\getattribute.js
      c:\python\selenium\webdriver\remote\isdisplayed.js
    Proceed (y/n)? y
      Successfully uninstalled selenium-3.4.2
    
    C:\Users\username>pip install -U selenium
    Collecting selenium
      Downloading selenium-3.9.0-py2.py3-none-any.whl (931kB)
        100% |¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦| 942kB 322kB/s
    Installing collected packages: selenium
    Successfully installed selenium-3.9.0
    
  • 确认已安装Selenium

    C:\Users\username>python
    Python 3.9.0 (v3.9.0:69c0db5, Jan 31 2018, 17:54:52) [MSC v.1900 32 bit (Intel)]
     on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    
    C:\Users\AtechM_03>pip freeze
    selenium==3.9.0
    
  • 打开一个IDE(例如Eclipse、PyCharm)并编写一个简单的程序,如下所示:

    FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Mike\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages\\selenium\\webdriver\\remote\\getAttribute.js'
    
    from selenium import webdriver
    
    driver = webdriver.Firefox(executable_path="C:\\path\\to\\geckodriver.exe")
    driver.get('https://stackoverflow.com')
    

  • 该错误确实给了我们一些关于发生了什么错误的提示,如下所示:

    FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Mike\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages\\selenium\\webdriver\\remote\\getAttribute.js'
    
    from selenium import webdriver
    
    driver = webdriver.Firefox(executable_path="C:\\path\\to\\geckodriver.exe")
    driver.get('https://stackoverflow.com')
    
    这本质上意味着getAttribute.js未在其预期位置找到

    getAttribute.js
    getAttribute.js
    文件在内部处理错误和其他关键JavaScript函数,如下所示:

    • 元素不可选择
    • 元素不可见
    • 无效的cookie域
    • 无效元素坐标
    • 无效元素状态
    • 无效选择器
    • 无效选择器
    • parseInt()
    • indexOf()
    • toUpperCase()
    • execScript()
    典型的
    getAttribute.js
    文件如下所示:

    function(){return function(){var d=this;function f(a){return"string"==typeof a};function h(a,b){this.code=a;this.a=l[a]||m;this.message=b||"";a=this.a.replace(/((?:^|\s+)[a-z])/g,function(a){return a.toUpperCase().replace(/^[\s\xa0]+/g,"")});b=a.length-5;if(0>b||a.indexOf("Error",b)!=b)a+="Error";this.name=a;a=Error(this.message);a.name=this.name;this.stack=a.stack||""}
    (function(){var a=Error;function b(){}b.prototype=a.prototype;h.b=a.prototype;h.prototype=new b;h.prototype.constructor=h;h.a=function(b,c,g){for(var e=Array(arguments.length-2),k=2;k<arguments.length;k++)e[k-2]=arguments[k];return a.prototype[c].apply(b,e)}})();var m="unknown error",l={15:"element not selectable",11:"element not visible"};l[31]=m;l[30]=m;l[24]="invalid cookie domain";l[29]="invalid element coordinates";l[12]="invalid element state";l[32]="invalid selector";l[51]="invalid selector";
    l[52]="invalid selector";l[17]="javascript error";l[405]="unsupported operation";l[34]="move target out of bounds";l[27]="no such alert";l[7]="no such element";l[8]="no such frame";l[23]="no such window";l[28]="script timeout";l[33]="session not created";l[10]="stale element reference";l[21]="timeout";l[25]="unable to set cookie";l[26]="unexpected alert open";l[13]=m;l[9]="unknown command";h.prototype.toString=function(){return this.name+": "+this.message};var n;a:{var p=d.navigator;if(p){var q=p.userAgent;if(q){n=q;break a}}n=""}function r(a){return-1!=n.indexOf(a)};function t(a,b){for(var e=a.length,c=f(a)?a.split(""):a,g=0;g<e;g++)g in c&&b.call(void 0,c[g],g,a)};function v(){return r("iPhone")&&!r("iPod")&&!r("iPad")};function w(){return(r("Chrome")||r("CriOS"))&&!r("Edge")};var x=r("Opera"),y=r("Trident")||r("MSIE"),z=r("Edge"),A=r("Gecko")&&!(-1!=n.toLowerCase().indexOf("webkit")&&!r("Edge"))&&!(r("Trident")||r("MSIE"))&&!r("Edge"),aa=-1!=n.toLowerCase().indexOf("webkit")&&!r("Edge");function B(){var a=d.document;return a?a.documentMode:void 0}var C;
    a:{var D="",E=function(){var a=n;if(A)return/rv\:([^\);]+)(\)|;)/.exec(a);if(z)return/Edge\/([\d\.]+)/.exec(a);if(y)return/\b(?:MSIE|rv)[: ]([^\);]+)(\)|;)/.exec(a);if(aa)return/WebKit\/(\S+)/.exec(a);if(x)return/(?:Version)[ \/]?(\S+)/.exec(a)}();E&&(D=E?E[1]:"");if(y){var F=B();if(null!=F&&F>parseFloat(D)){C=String(F);break a}}C=D}var G;var H=d.document;G=H&&y?B()||("CSS1Compat"==H.compatMode?parseInt(C,10):5):void 0;var ba=r("Firefox"),ca=v()||r("iPod"),da=r("iPad"),I=r("Android")&&!(w()||r("Firefox")||r("Opera")||r("Silk")),ea=w(),J=r("Safari")&&!(w()||r("Coast")||r("Opera")||r("Edge")||r("Silk")||r("Android"))&&!(v()||r("iPad")||r("iPod"));function K(a){return(a=a.exec(n))?a[1]:""}(function(){if(ba)return K(/Firefox\/([0-9.]+)/);if(y||z||x)return C;if(ea)return v()||r("iPad")||r("iPod")?K(/CriOS\/([0-9.]+)/):K(/Chrome\/([0-9.]+)/);if(J&&!(v()||r("iPad")||r("iPod")))return K(/Version\/([0-9.]+)/);if(ca||da){var a=/Version\/(\S+).*Mobile\/(\S+)/.exec(n);if(a)return a[1]+"."+a[2]}else if(I)return(a=K(/Android\s+([0-9.]+)/))?a:K(/Version\/([0-9.]+)/);return""})();var L,M=function(){if(!A)return!1;var a=d.Components;if(!a)return!1;try{if(!a.classes)return!1}catch(g){return!1}var b=a.classes,a=a.interfaces,e=b["@mozilla.org/xpcom/version-comparator;1"].getService(a.nsIVersionComparator),c=b["@mozilla.org/xre/app-info;1"].getService(a.nsIXULAppInfo).version;L=function(a){e.compare(c,""+a)};return!0}(),N=y&&!(8<=Number(G)),fa=y&&!(9<=Number(G));I&&M&&L(2.3);I&&M&&L(4);J&&M&&L(6);var ga={SCRIPT:1,STYLE:1,HEAD:1,IFRAME:1,OBJECT:1},O={IMG:" ",BR:"\n"};function P(a,b,e){if(!(a.nodeName in ga))if(3==a.nodeType)e?b.push(String(a.nodeValue).replace(/(\r\n|\r|\n)/g,"")):b.push(a.nodeValue);else if(a.nodeName in O)b.push(O[a.nodeName]);else for(a=a.firstChild;a;)P(a,b,e),a=a.nextSibling};function Q(a,b){b=b.toLowerCase();return"style"==b?ha(a.style.cssText):N&&"value"==b&&R(a,"INPUT")?a.value:fa&&!0===a[b]?String(a.getAttribute(b)):(a=a.getAttributeNode(b))&&a.specified?a.value:null}var ia=/[;]+(?=(?:(?:[^"]*"){2})*[^"]*$)(?=(?:(?:[^']*'){2})*[^']*$)(?=(?:[^()]*\([^()]*\))*[^()]*$)/;
    function ha(a){var b=[];t(a.split(ia),function(a){var c=a.indexOf(":");0<c&&(a=[a.slice(0,c),a.slice(c+1)],2==a.length&&b.push(a[0].toLowerCase(),":",a[1],";"))});b=b.join("");return b=";"==b.charAt(b.length-1)?b:b+";"}function S(a,b){N&&"value"==b&&R(a,"OPTION")&&null===Q(a,"value")?(b=[],P(a,b,!1),a=b.join("")):a=a[b];return a}function R(a,b){b&&"string"!==typeof b&&(b=b.toString());return!!a&&1==a.nodeType&&(!b||a.tagName.toUpperCase()==b)}
    function T(a){return R(a,"OPTION")?!0:R(a,"INPUT")?(a=a.type.toLowerCase(),"checkbox"==a||"radio"==a):!1};var ja={"class":"className",readonly:"readOnly"},U="async autofocus autoplay checked compact complete controls declare defaultchecked defaultselected defer disabled draggable ended formnovalidate hidden indeterminate iscontenteditable ismap itemscope loop multiple muted nohref noresize noshade novalidate nowrap open paused pubdate readonly required reversed scoped seamless seeking selected spellcheck truespeed willvalidate".split(" ");function V(a,b){var e=null,c=b.toLowerCase();if("style"==c)return(e=a.style)&&!f(e)&&(e=e.cssText),e;if(("selected"==c||"checked"==c)&&T(a)){if(!T(a))throw new h(15,"Element is not selectable");b="selected";e=a.type&&a.type.toLowerCase();if("checkbox"==e||"radio"==e)b="checked";return S(a,b)?"true":null}var g=R(a,"A");if(R(a,"IMG")&&"src"==c||g&&"href"==c)return(e=Q(a,c))&&(e=S(a,c)),e;if("spellcheck"==c){e=Q(a,c);if(null!==e){if("false"==e.toLowerCase())return"false";if("true"==e.toLowerCase())return"true"}return S(a,
    c)+""}g=ja[b]||b;a:if(f(U))c=f(c)&&1==c.length?U.indexOf(c,0):-1;else{for(var u=0;u<U.length;u++)if(u in U&&U[u]===c){c=u;break a}c=-1}if(0<=c)return(e=null!==Q(a,b)||S(a,g))?"true":null;try{var k=S(a,g)}catch(ka){}(c=null==k)||(c=typeof k,c="object"==c&&null!=k||"function"==c);c?e=Q(a,b):e=k;return null!=e?e.toString():null}var W=["_"],X=d;W[0]in X||!X.execScript||X.execScript("var "+W[0]);
    for(var Y;W.length&&(Y=W.shift());){var Z;if(Z=!W.length)Z=void 0!==V;Z?X[Y]=V:X[Y]&&X[Y]!==Object.prototype[Y]?X=X[Y]:X=X[Y]={}};; return this._.apply(null,arguments);}.apply({navigator:typeof window!='undefined'?window.navigator:null,document:typeof window!='undefined'?window.document:null}, arguments);}
    
  • 确保pip正常工作

    C:\Users\username>pip
    
    Usage:
      pip <command> [options]
    
    Commands:
      install                     Install packages.
      download                    Download packages.
      uninstall                   Uninstall packages.
      freeze                      Output installed packages in requirements format.
      list                        List installed packages.
      show                        Show information about installed packages.
      check                       Verify installed packages have compatible dependencies.
      search                      Search PyPI for packages.
      wheel                       Build wheels from your requirements.
      hash                        Compute hashes of package archives.
      completion                  A helper command used for command completion.
      help                        Show help for commands.
    
    General Options:
      -h, --help                  Show help.
      --isolated                  Run pip in an isolated mode, ignoring environment variables and user configuration.
      -v, --verbose               Give more output. Option is additive, and can be used up to 3 times.
      -V, --version               Show version and exit.
      -q, --quiet                 Give less output. Option is additive, and can be used up to 3 times (corresponding to WARNING, ERROR, and CRITICAL logging levels).
      --log <path>                Path to a verbose appending log.
      --proxy <proxy>             Specify a proxy in the form [user:passwd@]proxy.server:port.
      --retries <retries>         Maximum number of retries each connection should attempt (default 5 times).
      --timeout <sec>             Set the socket timeout (default 15 seconds).
      --exists-action <action>    Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort.
      --trusted-host <hostname>   Mark this host as trusted, even though it does not have valid or any HTTPS.
      --cert <path>               Path to alternate CA bundle.
      --client-cert <path>        Path to SSL client certificate, a single file containing the private key and the certificate in PEM format.
      --cache-dir <dir>           Store the cache data in <dir>.
      --no-cache-dir              Disable the cache.
      --disable-pip-version-check
                      Don't periodically check PyPI to determine
                      whether a new version of pip is available for
                  download. Implied with --no-index.
    
  • 通过pip安装最新的Selenium

    C:\Users\username>pip uninstall selenium
    Uninstalling selenium-3.4.2:
      c:\python\lib\site-packages\selenium-3.4.2.dist-info\description.rst
      c:\python\lib\site-packages\selenium-3.4.2.dist-info\installer
      c:\python\lib\site-packages\selenium-3.4.2.dist-info\metadata
      c:\python\lib\site-packages\selenium-3.4.2.dist-info\metadata.json
      c:\python\lib\site-packages\selenium-3.4.2.dist-info\record
      c:\python\lib\site-packages\selenium-3.4.2.dist-info\top_level.txt
      c:\python\lib\site-packages\selenium-3.4.2.dist-info\wheel
      c:\python\lib\site-packages\selenium\__init__.py
      c:\python\lib\site-packages\selenium\__pycache__\__init__.cpython-36.pyc
      c:\python\lib\site-packages\selenium\common\__init__.py
      .....
      .....
      .....  
      c:\python\lib\site-packages\selenium\webdriver\support\events.py
      c:\python\lib\site-packages\selenium\webdriver\support\expected_conditions.py
      c:\python\lib\site-packages\selenium\webdriver\support\select.py
      c:\python\lib\site-packages\selenium\webdriver\support\ui.py
      c:\python\lib\site-packages\selenium\webdriver\support\wait.py
      c:\python\selenium\webdriver\firefox\amd64\x_ignore_nofocus.so
      c:\python\selenium\webdriver\firefox\x86\x_ignore_nofocus.so
      c:\python\selenium\webdriver\remote\getattribute.js
      c:\python\selenium\webdriver\remote\isdisplayed.js
    Proceed (y/n)? y
      Successfully uninstalled selenium-3.4.2
    
    C:\Users\username>pip install -U selenium
    Collecting selenium
      Downloading selenium-3.9.0-py2.py3-none-any.whl (931kB)
        100% |¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦| 942kB 322kB/s
    Installing collected packages: selenium
    Successfully installed selenium-3.9.0
    
  • 确认已安装Selenium

    C:\Users\username>python
    Python 3.9.0 (v3.9.0:69c0db5, Jan 31 2018, 17:54:52) [MSC v.1900 32 bit (Intel)]
     on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    
    C:\Users\AtechM_03>pip freeze
    selenium==3.9.0
    
  • 打开一个IDE(例如Eclipse、PyCharm)并编写一个简单的程序,如下所示:

    FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Mike\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages\\selenium\\webdriver\\remote\\getAttribute.js'
    
    from selenium import webdriver
    
    driver = webdriver.Firefox(executable_path="C:\\path\\to\\geckodriver.exe")
    driver.get('https://stackoverflow.com')
    

  • 您必须从此处重新安装selenium-Windows版本


    看来装置坏了。如果要从repo安装,首先需要按照此处的步骤来构建所需的.js文件:

    您必须从此处重新安装selenium-Windows版本


    看来装置坏了。如果要从repo安装,首先需要按照此处的步骤来构建所需的.js文件:

    尝试先卸载,
    pip uninstall selenium
    ,然后重新安装ᴏʟᴅsᴘᴇᴇᴅ 我成功了!但奇怪的是看到文件被破坏。正在考虑升级我的笔记本电脑。不客气。我担心原因无法确定。有时,重新安装是唯一的解决办法。请尝试先卸载,
    pip uninstall selenium
    ,然后重新安装。现在执行此操作---@cᴏʟᴅsᴘᴇᴇᴅ 我成功了!但奇怪的是看到文件被破坏。正在考虑升级我的笔记本电脑。不客气。我担心原因无法确定。有时,重新安装是唯一的答案。@MikeMathMan不是。如果版本已经是最新的,这个答案就不会真正起作用,我不相信。@MikeMathMan不是。如果这个版本已经是最新的,这个答案就不会真正起作用。