ubuntu下安装selenium2.0 环境

参考:http://www.cnblogs.com/fnng/archive/2013/05/29/3106515.html

 

ubuntu 安装过程:

 

1、安装:setuptools

$ apt-get install python-setuptools

 

2、安装pip

1)源码安装方式(cd进python目录下)如下:

$ tar -zxvf pip-1.4.1.tar.gz

$ cd pip-1.4.1/ 

$ python setup.py install

2)这里直接使用apt-get安装:

$sudo apt-get install python-pip

参考:http://www.pip-installer.org/en/latest/installing.html

  注意,通过查看pip官网,V1.5.1以上版本,可以不安装python-setuptools

  安装后,可通过pip --version查看一下已安装的版本及安装路径。

 

3、安装selenium

进入pip安装目录下(如/usr/lib/python2.7/dist-packages/pip),执行下列语句:

$sudo pip install -U selenium

当出现
Successfully installed selenium
Cleaning up...

时说明安装成功。

4、验证selenium是否安装成功。

在ubuntu下,新建一个脚本pytest.py,脚本如下:

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time

browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://www.yahoo.com") # Load page
assert "Yahoo!" in browser.title
elem = browser.find_element_by_name("p") # Find the query box
elem.send_keys("seleniumhq" + Keys.RETURN)
time.sleep(0.2) # Let the page load, will be added to the API
try:
    browser.find_element_by_xpath("//a[contains(@href,'http://seleniumhq.org')]")
except NoSuchElementException:
    assert 0, "can't find seleniumhq"
browser.close()

 如果是上述脚本,则在终端下使用python /pytest.py 命令执行。

 若在脚本最开始前存在

#!/usr/bin/env python

则可以像sh文件一样执行。好像还需要对python设置成环境变量才成。

$ ./pytest.py

遇到的问题1:

python /pytest.py命令执行后,提示“IndentationError: unexpected unindent”

分析原因:脚本第一行未与其他行对齐。

遇到的问题2:

修改将代码对齐后,再次执行,又出现问题。如下所示:

Traceback (most recent call last):
  File "/testyjw/pytest.py", line 7, in <module>
    browser = webdriver.Firefox() # Get local session of firefox
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 59, in __init__
    self.binary, timeout),
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/extension_connection.py", line 47, in __init__
    self.binary.launch_browser(self.profile)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 61, in launch_browser
    self._wait_until_connectable()
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 100, in _wait_until_connectable
    self._get_firefox_output())
selenium.common.exceptions.WebDriverException: Message: "The browser appears to have exited before we could connect. The output was: \n(process:14510): GLib-CRITICAL **: g_slice_set_config: assertion `sys_page_size == 0' failed\nError: no display specified\n"

分析原因:

windows下使用ssh终端远程连接ubuntu,运行脚本存在上面问题。而直接在ubuntu上操作执行,则不存在问题。有待进一步分析。

 

posted @ 2014-01-23 18:05  milkty  阅读(4523)  评论(0编辑  收藏  举报