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上操作執行,則不存在問題。有待進一步分析。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM