Testing selenium 2和python 3.4.1的问题
我有一个简单的自动填写登录表单字段。事实上,它通过良好的,但有问题。我需要在脚本填充字段后在控制台中查看实际输出,如“登录成功”或“未找到用户名”。我尝试了很多东西,但都没有用这种方式,我最后一次尝试是while循环,效果很好,但只有当我得到积极的结果时。我写了第二个条件,但当我键入错误的数据时,在控制台中看到所有这些错误会让我发疯。下面是代码和部分输出Testing selenium 2和python 3.4.1的问题,testing,python-3.x,selenium-webdriver,Testing,Python 3.x,Selenium Webdriver,我有一个简单的自动填写登录表单字段。事实上,它通过良好的,但有问题。我需要在脚本填充字段后在控制台中查看实际输出,如“登录成功”或“未找到用户名”。我尝试了很多东西,但都没有用这种方式,我最后一次尝试是while循环,效果很好,但只有当我得到积极的结果时。我写了第二个条件,但当我键入错误的数据时,在控制台中看到所有这些错误会让我发疯。下面是代码和部分输出 from selenium import webdriver from selenium.webdriver.common.by import
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
baseurl = "http://www.somesite/login"
email = input("Type an email: ")
password = input("Type a password: ")
xpaths = { 'loginBox' : "//input[@id='session_email']",
'passwordBox' : "//input[@id='session_password']",
'submitButton' : "//input[@class='ufs-but']",
'success' : "//div[@class='flash-message success']",
'error' : "//span[@class='form_error']"
}
mydriver = webdriver.Firefox()
mydriver.get(baseurl)
mydriver.find_element_by_xpath(xpaths['loginBox']).send_keys(email)
mydriver.find_element_by_xpath(xpaths['passwordBox']).send_keys(password)
mydriver.find_element_by_xpath(xpaths['submitButton']).click()
while mydriver.find_element_by_xpath(xpaths['success']):
print("Success")
if mydriver.find_element_by_xpath(xpaths['error']):
print("No")
当我试图打断一个错误时,我得到了:
File "ab.py", line 32, in <module>
while mydriver.find_element_by_xpath(xpaths['success']):
File "/usr/local/lib/python3.4/site-packages/selenium-2.43.0-py3.4.egg/selenium/webdriver/remote/webdriver.py", line 230, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/usr/local/lib/python3.4/site-packages/selenium-2.43.0-py3.4.egg/selenium/webdriver/remote/webdriver.py", line 662, in find_element
{'using': by, 'value': value})['value']
File "/usr/local/lib/python3.4/site-packages/selenium-2.43.0-py3.4.egg/selenium/webdriver/remote/webdriver.py", line 173, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.4/site-packages/selenium-2.43.0-py3.4.egg/selenium/webdriver/remote/errorhandler.py", line 166, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: 'Unable to locate element: {"method":"xpath","selector":"//div[@class=\'flash-message success\']"}' ; Stacktrace:
at FirefoxDriver.prototype.findElementInternal_ (file:///tmp/tmpjax8kj1u/extensions/fxdriver@googlecode.com/components/driver-component.js:9618:26)
at FirefoxDriver.prototype.findElement (file:///tmp/tmpjax8kj1u/extensions/fxdriver@googlecode.com/components/driver-component.js:9627:3)
at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmpjax8kj1u/extensions/fxdriver@googlecode.com/components/command-processor.js:11612:16)
at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmpjax8kj1u/extensions/fxdriver@googlecode.com/components/command-processor.js:11617:7)
at DelayedCommand.prototype.execute/< (file:///tmp/tmpjax8kj1u/extensions/fxdriver@googlecode.com/components/command-processor.js:11559:5)
当我想要一个负的结果时,它可以工作,但不像我想要的那样:
Type an email: w
Type a password: wer
Success
No
正如你们所看到的,当结果是积极的时候,我想看到“成功”,当结果是消极的时候,我想看到“不”,但我不想同时看到它们
UPD。道具给我巨大的帮助,所以这就是我得到我想要的东西的原因:
try:
success = True
success_element = mydriver.find_element_by_xpath(xpaths['success'])
except NoSuchElementException:
success = False
print("Can't log in. Check email and/or password")
try:
failure = True
failure_element = mydriver.find_element_by_xpath(xpaths['error'])
except NoSuchElementException :
failure = False
print("Logged in successfully")
问题似乎在于您在最后构建while循环的方式。您不需要为了检查成功或失败而循环 假设您输入登录数据,则有四种结果。您可以找到决定成功的元素、决定失败的元素、两者都找到(应该不可能),或者两者都找不到(可能是在出现意外屏幕或加载页面失败的情况下) 不要期望webdriver查询返回一些值,而是尝试将它们放在
try
块中以捕获NoTouchElementException
并检查非None
内容。另外,请尝试处理这四种情况中的每一种,这样您的程序崩溃的频率就会降低
编辑:
试试这个
try:
success = True
success_element = mydriver.find_element_by_xpath(xpaths['success'])
except NoSuchElementException:
success = False
try:
failure = True
failure_element = mydriver.find_element_by_xpath(xpaths['error'])
except NoSuchElementException :
failure = False
# now handle the four possibilities
try:
success = True
success_element = mydriver.find_element_by_xpath(xpaths['success'])
except NoSuchElementException:
success = False
try:
failure = True
failure_element = mydriver.find_element_by_xpath(xpaths['error'])
except NoSuchElementException :
failure = False
# now handle the four possibilities