硒2和python的问题3.4.1

时间:2014-10-22 12:20:22

标签: testing python-3.x selenium-webdriver

我有一个简单的自动填充登录表单字段。实际上,它传递良好,但有问题。在脚本填充字段后,我需要在控制台中看到实际输出,例如“已成功登录”或“未找到用户名”。我尝试了很多东西,但没有任何方法可行,我的最后一次尝试是while循环,它运行良好,但只有当我有积极的结果。我写了第二个条件,但是当我输入不正确的数据时,在我的控制台中看到所有这些错误会让我发疯。所以这是代码和输出的一部分。

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) 

正如我所说,成功的结果不是问题。

UPD。我稍微纠正了我的代码的最后一部分,现在我有了这个:

while mydriver.find_element_by_xpath(xpaths['success']):
    print("Success")
    break
while mydriver.find_element_by_xpath(xpaths['error']):
    print("No")
    break

当我想要一个负面结果时,它可以工作,但不是我想要的输出:

Type an email: w
Type a password: wer
Success
No

如你所见,我希望在结果为正时看到'成功',在看到否定时看'不',但我不想同时看到它们。

UPD。道具向Macro Giancarli寻求巨大帮助,这就是我如何得到我想要的东西:

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")

1 个答案:

答案 0 :(得分:0)

问题看起来就像你在结束时构建while循环一样。您不应该循环以检查成功或失败。

假设您输入登录数据,请考虑有四种结果。您可以找到确定成功的元素,找到确定失败的元素,找到两者(应该是不可能的),或者找不到(可能是在意外屏幕的情况下,或者无法加载页面)。

不要期望从webdriver查询返回某些值,而是尝试将它们放在try块中以捕获NoSuchElementException并检查非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