로그인 폼 필드를 채우기위한 간단한 자동화 기능이 있습니다. 실제로, 그것은 잘 통과하지만, 문제가 있습니다. 스크립트가 "Logged in successfully"또는 "Username not found"와 같이 필드를 채운 후에 내 콘솔에서 실제 결과물을 볼 필요가 있습니다. 나는 많은 것들을 시도했지만이 방법으로는 아무 일도하지 않았습니다. 마지막 시도는 while 루프 였고 훌륭했습니다. 그러나 긍정적 인 결과가있을 때만 작동합니다. 나는 두 번째 조건을 썼지 만 잘못된 데이터를 입력하면 내 콘솔에서 이러한 모든 오류를 보게됩니다. 여기 코드와 출력 부분이 있습니다. 내가 말했듯이셀레늄 2 및 파이썬 3.4.1의 문제점
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/[email protected]/components/driver-component.js:9618:26)
at FirefoxDriver.prototype.findElement (file:///tmp/tmpjax8kj1u/extensions/[email protected]/components/driver-component.js:9627:3)
at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmpjax8kj1u/extensions/[email protected]/components/command-processor.js:11612:16)
at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmpjax8kj1u/extensions/[email protected]/components/command-processor.js:11617:7)
at DelayedCommand.prototype.execute/< (file:///tmp/tmpjax8kj1u/extensions/[email protected]/components/command-processor.js:11559:5)
, 성공적인 결과는 문제가되지 않습니다 : 나는 오류를 중단 할 때
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")
는 내가 무엇을 가지고있다.
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에 소품, 즉 내가 정확히 원하는 것을 얻었는지, 그래서 : 당신이 마지막에 당신의 while 루프를 구성하는 방식처럼
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")