Selenium / Python: Chrome的關閉警告窗口。

[英]Selenium / Python : Closing Alert Window in Chrome


Issue: Unable to close alert box in Chrome.

問題:無法在Chrome中關閉警告框。

Situation: I need to close an alert in the Chrome browser. A series of tests will trigger an alert to the user, like: "You forgot your first name." In order to go back to the user name field and enter the text, I must first close the alert. This works perfectly in FireFox, but the alert in Chrome is ignored. I have read numerous entries on this, and none work.

情境:我需要在Chrome瀏覽器中關閉一個警告。一系列測試將觸發對用戶的警告,比如:“您忘記了您的名字。”為了返回到用戶名字段並輸入文本,我必須首先關閉警報。這在FireFox中非常有效,但是Chrome的警告被忽略了。我已經讀了很多關於這個的文章,沒有一個工作。

Test Code:

測試代碼:

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

Full Error:

完整的錯誤:

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

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

1 个解决方案

#1


1  

Actually, Amey pointed out my ignorance. I had updated Selenium and other items, but not chromedriver. Major duh on my part. I"ll remember to check everything before posting again.

事實上,艾米指出了我的無知。我已經更新了Selenium和其他項目,但沒有更新chromedriver。我的主要任務。我記得在再投寄之前檢查所有的東西。

Thanks, Amey!

謝謝,Amey公司!


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/09/04/72076661c4eb82e62bfb41699563f2b7.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com