Python和Selenium - 安装不起作用

时间:2011-11-21 11:04:06

标签: python selenium-webdriver

使用-Python 2.5 操作系统 - Windows Vista 我尝试使用

安装Selenium 2.13时出错
  

pip install -U Selenium

C:\Users\User>pip install -U Selenium
Downloading/unpacking Selenium
Downloading selenium-2.13.1.tar.gz (3.3Mb): 3.3Mb downloaded
Running setup.py egg_info for package Selenium
warning: no files found matching 'docs\api\py\index.rst'
c:\Python25\lib\distutils\dist.py:263: UserWarning: Unknown distribution option: 'src_root'
  warnings.warn(msg)
Downloading/unpacking rdflib==3.1.0 (from Selenium)
Downloading rdflib-3.1.0.tar.gz (249Kb): 249Kb downloaded
Running setup.py egg_info for package rdflib
Installing collected packages: Selenium, rdflib
Found existing installation: selenium 2.13.1
Uninstalling selenium:
  Successfully uninstalled selenium
Running setup.py install for Selenium
warning: no files found matching 'docs\api\py\index.rst'
c:\Python25\lib\distutils\dist.py:263: UserWarning: Unknown distribution option: 'src_root'
  warnings.warn(msg)
c:\Python25\Lib\site-packages\selenium\webdriver\remote\webdriver.py:668: Warning: 'with' will  become a reserved keyword in Python 2.6
File "c:\Python25\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 668 with  open(filename, 'wb') as f:
            ^
SyntaxError: invalid syntax

Found existing installation: rdflib 3.1.0
Uninstalling rdflib:
Successfully uninstalled rdflib
Running setup.py install for rdflib
Successfully installed Selenium rdflib
Cleaning up...

因此手动下载了Selenium 2.13软件包并将selenium.py文件复制到c:/ python25 / lib /中。但是,当我尝试从python中导入webdriver时,它给出了以下错误:

C:\Users\User>python F:\Selenium\localtest2.py
Traceback (most recent call last):
  File "F:\Selenium\localtest2.py", line 1, in <module>
    from selenium import webdriver
ImportError: cannot import name webdriver

selenium安装是否有问题,或者我有什么问题吗?也许只复制selenium.py文件是愚蠢的?新手,忍受我。遵循给定here的教程。

在上面的教程中我不明白如何做到这一点:“将文件selenium.py添加到测试的路径中”

2 个答案:

答案 0 :(得分:2)

那个安装手册对我来说似乎有些尴尬。首先,删除所有文件(尝试pip uninstall Selenium,或者如果不起作用,只需从selenium文件夹中删除site-packages文件夹即可。另外,删除您复制的单个selenium.py文件。

然后,再次运行pip install -U selenium。如果你可以打开python提示并执行以下操作而没有错误,那就很好。

from selenium import webdriver

请注意,您还需要Selenium server,安装如下:

java -jar selenium-server-standalone-2.13.0.jar

如果所有这些都不起作用,请像以前一样清理您的网站包,然后下载并安装Python package index上找到的文件。解压缩后,打开命令提示符,在其中解压缩文件并运行python setup.py install。然后,尝试再次从命令提示符导入selenium,如上所述。

答案 1 :(得分:2)

显然Selenium 2.13需要Python 2.6,因为语法错误(依赖:'with')。

您可以选择安装早期版本的Selenium,或者更好的是,在Windows计算机上升级到Python 2.6。