我录制了一个工作脚本(参见本问题末尾的附件),并将其导出为“python 2 / unittest / WebDriver”.但是,源代码清楚地表明它存在一些问题(带有令人不舒服的“ERROR”语句的注释行):
# -*- Coding: utf-8 -*-from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.support.ui import Selectfrom selenium.common.exceptions import NoSuchElementExceptionfrom selenium.common.exceptions import NoAlertPresentExceptionimport unittest,time,reclass 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/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.166sFailed (errors=1)
我是否必须手动修复所有错误(如果是,如何?)?不能“Selenium IDE”导出WebDriver脚本的工作版本?我需要安装其他东西吗?我的一般做法完全错了吗?也许我应该使用别的东西而不是Selenium?
这是最初工作的Selenium IDE Testcase脚本.
<?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>解决方法 不幸的是,Selenium IDE不是一个完美的自动化测试工具,只是作为初学者的开始,这就是为什么它生成的大部分代码可能包含过时或错误的代码(在你的情况下是错误的命令).
您需要手动处理每个例外.
我知道它看起来很蹩脚,但看看documentation Selenium Python
它将帮助您以适当的形式重写这些方法.希望这会扼杀你
总结以上是内存溢出为你收集整理的如何从Selenium IDE创建一个有效的Selenium WebDriver python脚本?全部内容,希望文章能够帮你解决如何从Selenium IDE创建一个有效的Selenium WebDriver python脚本?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)