1

I'm having a problem with a Cancel button which can be clicked manually, but fails when I try to get Selenium to click on it.

The button is being identified, but when I assert if the button is clickable, it says that it's not clickable (despite the button being enabled and me being able to click it manually.

We're unable to see any difference between the properties of this button and the cancel buttons on other windows (besides the ID), but only this one seems to be not clickable.

Has anyone come across an issue like this before?

<button id = "sending-manual-cancel" class="btn btn danger" type="button" ng-click="cancel(true)">

Stack trace looks as follows:

self = response = {'sessionId': '4e0906ecb402b33e58384ddd4d55e714', 'status': 11, 'value': {'message': 'element not visible\n (Session ...ver info: chromedriver=2.22.397933 (1cab651507b88dec79b2b2a22d1943c01833cc1b),platform=Windows NT 10.0.10586 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.get('status', None)
    if status is None or status == ErrorCode.SUCCESS:
        return

    value = None
    message = response.get("message", "")
    screen = response.get("screen", "")
    stacktrace = None
    if isinstance(status, int):
        value_json = response.get('value', None)
        if value_json and isinstance(value_json, basestring):
            import json
            try:
                value = json.loads(value_json)
                status = value.get('error', None)
                if status is None:
                    status = value["status"]
                    message = value["value"]
                    if not isinstance(message, basestring):
                        value = message
                        try:
                            message = message['message']
                        except TypeError:
                            message = None
                else:
                    message = value.get('message', None)
            except ValueError:
                pass

    exception_class = ErrorInResponseException
    if status in ErrorCode.NO_SUCH_ELEMENT:
        exception_class = NoSuchElementException
    elif status in ErrorCode.NO_SUCH_FRAME:
        exception_class = NoSuchFrameException
    elif status in ErrorCode.NO_SUCH_WINDOW:
        exception_class = NoSuchWindowException
    elif status in ErrorCode.STALE_ELEMENT_REFERENCE:
        exception_class = StaleElementReferenceException
    elif status in ErrorCode.ELEMENT_NOT_VISIBLE:
        exception_class = ElementNotVisibleException
    elif status in ErrorCode.INVALID_ELEMENT_STATE:
        exception_class = InvalidElementStateException
    elif status in ErrorCode.INVALID_SELECTOR \
            or status in ErrorCode.INVALID_XPATH_SELECTOR \
            or status in ErrorCode.INVALID_XPATH_SELECTOR_RETURN_TYPER:
        exception_class = InvalidSelectorException
    elif status in ErrorCode.ELEMENT_IS_NOT_SELECTABLE:
        exception_class = ElementNotSelectableException
    elif status in ErrorCode.INVALID_COOKIE_DOMAIN:
        exception_class = WebDriverException
    elif status in ErrorCode.UNABLE_TO_SET_COOKIE:
        exception_class = WebDriverException
    elif status in ErrorCode.TIMEOUT:
        exception_class = TimeoutException
    elif status in ErrorCode.SCRIPT_TIMEOUT:
        exception_class = TimeoutException
    elif status in ErrorCode.UNKNOWN_ERROR:
        exception_class = WebDriverException
    elif status in ErrorCode.UNEXPECTED_ALERT_OPEN:
        exception_class = UnexpectedAlertPresentException
    elif status in ErrorCode.NO_ALERT_OPEN:
        exception_class = NoAlertPresentException
    elif status in ErrorCode.IME_NOT_AVAILABLE:
        exception_class = ImeNotAvailableException
    elif status in ErrorCode.IME_ENGINE_ACTIVATION_FAILED:
        exception_class = ImeActivationFailedException
    elif status in ErrorCode.MOVE_TARGET_OUT_OF_BOUNDS:
        exception_class = MoveTargetOutOfBoundsException
    else:
        exception_class = WebDriverException
    if value == '' or value is None:
        value = response['value']
    if isinstance(value, basestring):
        if exception_class == ErrorInResponseException:
            raise exception_class(response, value)
        raise exception_class(value)
    if message == "" and '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)
    elif exception_class == UnexpectedAlertPresentException and 'alert' in value:
        raise exception_class(message, screen, stacktrace, value['alert'].get('text'))

raise exception_class(message, screen, stacktrace) selenium.common.exceptions.ElementNotVisibleException: Message: element not visible E (Session info: chrome=51.0.2704.103) E
(Driver info: chromedriver=2.22.397933 (1cab651507b88dec79b2b2a22d1943c01833cc1b),platform=Windows NT 10.0.10586 x86_64)

C:\Python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py:194: ElementNotVisibleException

  • See what is clickable checks, maybe there is some attribute missing.As an alternative you can check that the button is enabled. – lauda Aug 24 '16 at 13:11
  • It's fine when I check if it's enabled, only hits a problem when I do "WebDriverWait(self.driver, 2).until(EC.element_to_be_clickable(By.ID(button_name)))" – ChrisG29 Aug 24 '16 at 13:41
  • As i see element_to_be_clickable check if it is visible and enabled.Maybe it is not visible.What is the exact error/stack-trace? – lauda Aug 24 '16 at 14:34
  • Also you might have some incorrect syntax. Check here selenium-python.readthedocs.io/waits.html – lauda Aug 24 '16 at 14:38
  • @Chris, can you post full error? – George Aug 24 '16 at 14:59
1

It seems that element is not visible at the time selenium wants to click it. And it is different than if it was visible but not clickable.

Try adding explicit wait for element to be visible, or implicit wait right before you assert if its clickable / click it. Then check if that solves problem or not. If it does then write better checks before actions.

If it doesnt there is a problem with locating element.

  • I have an implicit wait there: WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.ID, buttonName))) the next line in the code stores it as a variable: mybutton=self.driver.find_element_by_id(buttonName) which all works fine. It's only when I try: mybutton.click() that I get the stack trace – ChrisG29 Aug 25 '16 at 11:16
  • If you can, try waiting for visibility_of_element_located and then element_to_be_clickable, before using click() - (and note if there any errors and where) presence_of_element_located does not mean its visible or can be clicked. – George Aug 25 '16 at 12:19
  • i think there is a wait for clickable try this wait.until(EC.elementToBeClickable((By.ID, buttonName))) – mosaad Aug 30 '16 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.