2015-10-15 42 views
3

我是新來的硒,我嘗試使用Selenium IDE(2.9.0)創建第一個點擊和記錄腳本作爲基本,然後我使用Selenium WebDriver(2.48.0)進行了改進。如何從Selenium IDE創建一個可用的Selenium WebDriver python腳本?

我記錄了一個工作腳本(參見本問題末尾),並將其導出爲「python 2/unittest/WebDriver」。但是,源代碼清楚,有(註釋行以令人不安的「ERROR」語句)會有問題了:

# -*- coding: utf-8 -*- 
from selenium import webdriver 
from selenium.webdriver.common.by import By 
from selenium.webdriver.common.keys import Keys 
from selenium.webdriver.support.ui import Select 
from selenium.common.exceptions import NoSuchElementException 
from selenium.common.exceptions import NoAlertPresentException 
import unittest, time, re 

class Test1(unittest.TestCase): 
    def setUp(self): 
     self.driver = webdriver.Firefox() 
     self.driver.implicitly_wait(30) 
     self.base_url = "http://ironspider.ca/" 
     self.verificationErrors = [] 
     self.accept_next_alert = True 

    def test_1(self): 
     driver = self.driver 
     driver.get(self.base_url + "/frames/frames_example1/advanced.htm") 
     # ERROR: Caught exception [ERROR: Unsupported command [selectFrame | content | ]] 
     self.assertEqual("The Eve of the War", driver.find_element_by_css_selector("h2").text) 
     # ERROR: Caught exception [ERROR: Unsupported command [selectWindow | name=menu | ]] 
     driver.find_element_by_link_text("Chapter 2").click() 
     # ERROR: Caught exception [ERROR: Unsupported command [selectWindow | name=content | ]] 
     self.assertEqual("The Falling Star", driver.find_element_by_css_selector("h2").text) 

    def is_element_present(self, how, what): 
     try: self.driver.find_element(by=how, value=what) 
     except NoSuchElementException: return False 
     return True 

    def is_alert_present(self): 
     try: self.driver.switch_to_alert() 
     except NoAlertPresentException: return False 
     return True 

    def close_alert_and_get_its_text(self): 
     try: 
      alert = self.driver.switch_to_alert() 
      alert_text = alert.text 
      if self.accept_next_alert: 
       alert.accept() 
      else: 
       alert.dismiss() 
      return alert_text 
     finally: self.accept_next_alert = True 

    def tearDown(self): 
     self.driver.quit() 
     self.assertEqual([], self.verificationErrors) 

if __name__ == "__main__": 
    unittest.main() 

運行此代碼也沒有工作。錯誤是:

ERROR: test_1 (__main__.Test1) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "E:\Privat\Learn\Selenium\test1.py", line 22, in test_1 
    self.assertEqual("The Eve of the War", driver.find_element_by_css_selector("h2").text) 
    File "C:\Program Files (x86)\Python 3.5\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 402, in find_element_by_css_selector 
    return self.find_element(by=By.CSS_SELECTOR, value=css_selector) 
    File "C:\Program Files (x86)\Python 3.5\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 712, in find_element 
    {'using': by, 'value': value})['value'] 
    File "C:\Program Files (x86)\Python 3.5\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 201, in execute 
    self.error_handler.check_response(response) 
    File "C:\Program Files (x86)\Python 3.5\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 181, in check_response 
    raise exception_class(message, screen, stacktrace) 
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"css selector","selector":"h2"} 
Stacktrace: 
    at FirefoxDriver.prototype.findElementInternal_ (file:///C:/Users/dial1/AppData/Local/Temp/tmpu1otxnnn/extensions/[email protected]/components/driver-component.js:10659) 
    at fxdriver.Timer.prototype.setTimeout/<.notify (file:///C:/Users/dial1/AppData/Local/Temp/tmpu1otxnnn/extensions/[email protected]/components/driver-component.js:621) 

---------------------------------------------------------------------- 
Ran 1 test in 34.166s 

FAILED (errors=1) 

我必須手動修復所有的錯誤(如果是這樣,怎麼辦?)?不能「硒IDE」導出工作版本的WebDriver腳本?我需要安裝其他東西嗎?我的一般方法是完全錯誤的嗎?也許我應該使用硒以外的東西?

這裏是原來的工作硒IDE測試腳本。

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head profile="http://selenium-ide.openqa.org/profiles/test-case"> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<link rel="selenium.base" href="http://ironspider.ca/" /> 
<title>example1</title> 
</head> 
<body> 
<table cellpadding="1" cellspacing="1" border="1"> 
<thead> 
<tr><td rowspan="1" colspan="3">example1</td></tr> 
</thead><tbody> 
<tr> 
    <td>open</td> 
    <td>/frames/frames_example1/advanced.htm</td> 
    <td></td> 
</tr> 
<tr> 
    <td>selectFrame</td> 
    <td>content</td> 
    <td></td> 
</tr> 
<tr> 
    <td>assertText</td> 
    <td>css=h2</td> 
    <td>The Eve of the War</td> 
</tr> 
<tr> 
    <td>selectWindow</td> 
    <td>name=menu</td> 
    <td></td> 
</tr> 
<tr> 
    <td>click</td> 
    <td>link=Chapter 2</td> 
    <td></td> 
</tr> 
<tr> 
    <td>selectWindow</td> 
    <td>name=content</td> 
    <td></td> 
</tr> 
<tr> 
    <td>pause</td> 
    <td>500</td> 
    <td></td> 
</tr> 
<tr> 
    <td>assertText</td> 
    <td>css=h2</td> 
    <td>The Falling Star</td> 
</tr> 

</tbody></table> 
</body> 
</html> 
+0

可能的重複[爲什麼我們使用WebDriver而不是Selenium IDE?](http://stackoverflow.com/questions/19683100/why-do-we-use-webdriver-instead-of- selenium-ide) – Louis

回答

1

不幸的是硒IDE也不是自動化測試的理想工具,只能作爲初學者一開始,這就是爲什麼代碼的很大一部分會生成可能包含過時,或只是錯誤代碼(錯誤的命令,你的情況)。 您將需要手動處理每個例外情況。

我知道這看起來有點假,但是看看documentation Selenium Python

它會幫助你重寫以適當的形式的方法。希望這會幫助你

+1

非常感謝。我會看看文檔的部分... – Alex