问题:无法关闭Chrome中的提醒框。
情况:我需要在Chrome浏览器中关闭提醒。一系列测试将触发用户警报,例如:“您忘记了您的名字。”为了返回用户名字段并输入文本,我必须先关闭警报。这在FireFox中完美运行,但Chrome中的警报会被忽略。我已经阅读了很多条目,但都没有。
测试代码:
from selenium import webdriver
import selenium.webdriver.chrome.webdriver
import selenium.webdriver.common.alert
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re
class ChallengeTests(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(5)
self.base_url = "https://www.test.com"
self.verificationErrors = []
self.accept_next_alert = True
# MISSING FIRST NAME
def test_01_missing_first_name(self):
driver = self.driver
driver.get(self.base_url + "/")
#driver.find_element_by_id("fname").send_keys("Foo")
driver.find_element_by_id("lname").send_keys("Bar")
driver.find_element_by_id("screenname").send_keys("EvilMonkey")
driver.find_element_by_id("password").send_keys("test")
driver.find_element_by_id("password2").send_keys("test")
driver.find_element_by_id("reg_button").click()
alert = self.driver.switch_to_alert()
alert.dismiss()
driver.find_element_by_id("fname").send_keys("Foo")
driver.find_element_by_id("reg_button").click()
完全错误:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <selenium.webdriver.common.alert.Alert object at 0x10a812d10>
def dismiss(self):
"""
Dismisses the alert available.
"""
> self.driver.execute(Command.DISMISS_ALERT)
/Library/Python/2.7/site-packages/selenium/webdriver/common/alert.py:48:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <selenium.webdriver.chrome.webdriver.WebDriver object at 0x10a812f10>, driver_command = 'dismissAlert'
params = {'sessionId': u'3eebbe1b12fa415ddc00d99cdb35569e'}
def execute(self, driver_command, params=None):
"""
Sends a command to be executed by a command.CommandExecutor.
:Args:
- driver_command: The name of the command to execute as a string.
- params: A dictionary of named parameters to send with the command.
:Returns:
The command's JSON response loaded into a dictionary object.
"""
if not params:
params = {'sessionId': self.session_id}
elif 'sessionId' not in params:
params['sessionId'] = self.session_id
params = self._wrap_value(params)
response = self.command_executor.execute(driver_command, params)
if response:
> self.error_handler.check_response(response)
/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py:165:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <selenium.webdriver.remote.errorhandler.ErrorHandler object at 0x10a812f90>
response = {u'sessionId': u'3eebbe1b12fa415ddc00d99cdb35569e', u'status': 13, u'value': {u'message': u'unknown error: unhandled i... {"code":-32603,"message":"No JavaS....0.1547.65)\n (Driver info: chromedriver=0.8,platform=Mac OS X 10.8.4 x86_64)'}}
def check_response(self, response):
"""
Checks that a JSON response from the WebDriver does not have an error.
:Args:
- response - The JSON response from the WebDriver server as a dictionary
object.
:Raises: If the response contains an error message.
"""
status = response['status']
if status == ErrorCode.SUCCESS:
return
exception_class = ErrorInResponseException
if status == ErrorCode.NO_SUCH_ELEMENT:
exception_class = NoSuchElementException
elif status == ErrorCode.NO_SUCH_FRAME:
exception_class = NoSuchFrameException
elif status == ErrorCode.NO_SUCH_WINDOW:
exception_class = NoSuchWindowException
elif status == ErrorCode.STALE_ELEMENT_REFERENCE:
exception_class = StaleElementReferenceException
elif status == ErrorCode.ELEMENT_NOT_VISIBLE:
exception_class = ElementNotVisibleException
elif status == ErrorCode.INVALID_ELEMENT_STATE:
exception_class = InvalidElementStateException
elif status == ErrorCode.INVALID_SELECTOR \
or status == ErrorCode.INVALID_XPATH_SELECTOR \
or status == ErrorCode.INVALID_XPATH_SELECTOR_RETURN_TYPER:
exception_class = InvalidSelectorException
elif status == ErrorCode.ELEMENT_IS_NOT_SELECTABLE:
exception_class = ElementNotSelectableException
elif status == ErrorCode.INVALID_COOKIE_DOMAIN:
exception_class = WebDriverException
elif status == ErrorCode.UNABLE_TO_SET_COOKIE:
exception_class = WebDriverException
elif status == ErrorCode.TIMEOUT:
exception_class = TimeoutException
elif status == ErrorCode.SCRIPT_TIMEOUT:
exception_class = TimeoutException
elif status == ErrorCode.UNKNOWN_ERROR:
exception_class = WebDriverException
elif status == ErrorCode.UNEXPECTED_ALERT_OPEN:
exception_class = UnexpectedAlertPresentException
elif status == ErrorCode.NO_ALERT_OPEN:
exception_class = NoAlertPresentException
elif status == ErrorCode.IME_NOT_AVAILABLE:
exception_class = ImeNotAvailableException
elif status == ErrorCode.IME_ENGINE_ACTIVATION_FAILED:
exception_class = ImeActivationFailedException
elif status == ErrorCode.MOVE_TARGET_OUT_OF_BOUNDS:
exception_class = MoveTargetOutOfBoundsException
else:
exception_class = WebDriverException
value = response['value']
if isinstance(value, basestring):
if exception_class == ErrorInResponseException:
raise exception_class(response, value)
raise exception_class(value)
message = ''
if 'message' in value:
message = value['message']
screen = None
if 'screen' in value:
screen = value['screen']
stacktrace = None
if 'stackTrace' in value and value['stackTrace']:
stacktrace = []
try:
for frame in value['stackTrace']:
line = self._value_or_default(frame, 'lineNumber', '')
file = self._value_or_default(frame, 'fileName', '<anonymous>')
if line:
file = "%s:%s" % (file, line)
meth = self._value_or_default(frame, 'methodName', '<anonymous>')
if 'className' in frame:
meth = "%s.%s" % (frame['className'], meth)
msg = " at %s (%s)"
msg = msg % (meth, file)
stacktrace.append(msg)
except TypeError:
pass
if exception_class == ErrorInResponseException:
raise exception_class(response, message)
> raise exception_class(message, screen, stacktrace)
E WebDriverException: Message: u'unknown error: unhandled inspector error: {"code":-32603,"message":"No JavaScript dialog to handle"}\n (Session info: chrome=29.0.1547.65)\n (Driver info: chromedriver=0.8,platform=Mac OS X 10.8.4 x86_64)'
/Library/Python/2.7/site-packages/selenium/webdriver/remote/errorhandler.py:164: WebDriverException
答案 0 :(得分:1)
实际上,Amey指出了我的无知。我更新了Selenium和其他物品,但不是chromedriver。主要是我。我会记得在再次发布之前检查everything
。
谢谢,Amey!