我是selenium的新手,我正尝试使用Selenium IDE(2.9.0)创建第一个单击记录脚本作为基本脚本,然后使用Selenium WebDriver(2.48.0)对其进行完善。
我记录了一个工作脚本(请参阅本问题末尾的附件),并将其导出为“ 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()
Run Code Online (Sandbox Code Playgroud)
运行此代码也不起作用。错误是:
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/fxdriver@googlecode.com/components/driver-component.js:10659)
at fxdriver.Timer.prototype.setTimeout/<.notify (file:///C:/Users/dial1/AppData/Local/Temp/tmpu1otxnnn/extensions/fxdriver@googlecode.com/components/driver-component.js:621)
----------------------------------------------------------------------
Ran 1 test in 34.166s
FAILED (errors=1)
Run Code Online (Sandbox Code Playgroud)
我是否必须手动修复所有错误(如果是,如何解决?)?“ Selenium 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>
Run Code Online (Sandbox Code Playgroud)
小智 1
不幸的是,Selenium IDE 并不是一个完美的自动化测试工具,只能作为初学者的开始,这就是为什么它生成的大部分代码可能包含过时的或错误的代码(在您的情况下是错误的命令)。您需要手动处理每个异常。
我知道它看起来很蹩脚,但是请看一下Selenium Python 文档
它将帮助您以正确的形式重写这些方法。希望这对你有帮助
| 归档时间: |
|
| 查看次数: |
1007 次 |
| 最近记录: |