18

When running Selenium tests on my Django project, I've started to get the error:

selenium.common.exceptions.WebDriverException: Message: Element is not clickable at point (61, 24.300003051757812). Other element would receive the click: <a class="navbar-brand" href="#"></a>

It is odd for two reasons: First, the tests previously passed and I have not edited that part of the code base. Second, when the Selenium-driven Firefox Window pops up and I maximize the page, the tests pass. But when I let the Selenium tests run with the Firefox browser not maximized, they fail.

I'm not using any fancy javascript (just the basic Bootstrap 3 template), just plain old html and css. I'm using Django 1.9 on Python 3.4. I've run pip to check for upgrades to Selenium, and I'm up to date.

Here is a pastebin link to the html of the output of my view and template.

One of the failing tests is:

def test_create_task_and_check_that_it_shows_up_in_the_task_manager_index_and_filter(self):
        # Create user
        self.user = User.objects.get(username=test_superuser_username)
        # Log the user in
        self.log_user_in(user_object=self.user, password=test_superuser_password)
        self.browser.implicitly_wait(10)
        # Pull up the main task manager page
        self.browser.get(str(self.live_server_url) + reverse('task_manager:index'))
        # Make sure we go to the task manager index
        task_index_url = str(self.live_server_url) + reverse('task_manager:index')
        self.browser.get(task_index_url)
        self.browser.implicitly_wait(4)
        self.assertTrue(str(task_index_url) == self.browser.current_url,
                        msg=('Assertion that current_url is %s failed. Current_url is %s' %
                             (str(reverse('task_manager:index')), self.browser.current_url)))
        # Click the 'add task' button on the sidebar
        add_task_taskbar_button = self.browser.find_element_by_name('add_task_sidebar_link')
        add_task_taskbar_button.click()

The last line produces the error:

ERROR: test_create_task_and_check_that_it_shows_up_in_the_task_manager_index_and_filter (tasks.tests.test_functional.SeleniumTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/mint/Python_Projects/[project_name]/tasks/tests/test_functional.py", line 94, in test_create_task_and_check_that_it_shows_up_in_the_task_manager_index_and_filter
    add_task_taskbar_button.click()
  File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/selenium/webdriver/remote/webelement.py", line 75, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/selenium/webdriver/remote/webelement.py", line 469, in _execute
    return self._parent.execute(command, params)
  File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/selenium/webdriver/remote/webdriver.py", line 201, in execute
    self.error_handler.check_response(response)
  File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Element is not clickable at point (61, 24.300003051757812). Other element would receive the click: <a class="navbar-brand" href="#"></a>
jejy2343
  • 449
  • 2
  • 6
  • 18

7 Answers7

30

Extremely late to the party, but I have an extremely simple solution that worked for me. Instead of doing .click(), simply do

.send_keys(selenium.webdriver.common.keys.Keys.SPACE)

With the element selected, the spacebar can toggle the selection. Worked for me!

Major Major
  • 2,697
  • 3
  • 27
  • 35
11

Your Answer lies within your question.

  1. Error says 'add_task_sidebar_link' not clickable at point - (61, 24.300003051757812) This is where another element is - Other element would receive the click: <a class="navbar-brand" href="#"></a> where the click is being attempted. Since you did not maximize the browser, you are not able to get the point co ordinates correctly.
  2. In order for future tests to not break, pls scroll to that element. Refer to this post (Selenium python unable to scroll down). Check Action chains (http://selenium-python.readthedocs.org/api.html#module-selenium.webdriver.common.action_chains)
Community
  • 1
  • 1
Makjb lh
  • 432
  • 5
  • 16
4

Ran into a similar problem (same error) and tried Major Major's solution above using send_keys + sending the space bar to simulate a click rather than a mouse click.

Major Major's solution:

.send_keys(selenium.webdriver.common.keys.Keys.SPACE)

I hit a new error trying to run this, pointing at 'selenium'. Removing that keyword from the code fixed the issue, and allowed me to click the element.

Final successful code snippet used:

my_variable = driver.find_element_by_xpath('//*[@id="myId"]') #this is the checkbox
my_variable.send_keys(webdriver.common.keys.Keys.SPACE)
Will Rus
  • 41
  • 2
0

This has more to do with elements 'covering' each other. Which makes sense if it's happening on non-maximized windows. Could also happen if there was a popup/floating div or another element which covering the element you're actually trying to click on.

Remember, selenium is mimicking the user, so you can't normally do an action which the user wouldn't have been able to do - like click on an element which is covered by another.

A potential workaround for this would be to use Javascript to find the element and click on it. Example here:

labels = driver.find_elements_by_tag_name("label")
inputs = driver.execute_script(
    "var labels = arguments[0], inputs = []; for (var i=0; i < labels.length; i++){" +
    "inputs.push(document.getElementById(labels[i].getAttribute('for'))); } return inputs;", labels)
aneroid
  • 12,983
  • 3
  • 36
  • 66
  • @ aneroid If above is true - the tests **should probably Fail**. @ jejy2343 - The above confirms a code defect in your application. I am guessing when you don't maximize your elements should automatically be either 1. scrollable and maintain its span vertically/horizontally or 2. re-arrange itself - applicable for cross platform Web UI. Either ONE of them should happen. When you use above answer using Javascript you are by passing a potential health check and bypassing that defect. The reverse may also true - your test case is potentially insufficient is you tried to click it. – Makjb lh Feb 25 '16 at 19:45
  • Lemme summarize above:: A Web UI should not overlay its elements where a user is trying to use it . I would say it can be considered as a Look and Feel defect – Makjb lh Feb 25 '16 at 19:51
  • Thanks for the quick answer. I think what might be happening is that when the Firefox window is not maximized, it's scrolling down to where the link would be in the top left corner. But the navbar is set to be at the top of the page, even when you're scrolling, so it's also in the top left corner. That's a guess--I'm not exactly sure how selenium handles scrolling when selecting links. – jejy2343 Feb 25 '16 at 20:16
  • I am not sure if Selenium is automatically scrolling to that element. Try executing and verify. Also try to scroll explicitly . Another thing try this - display the co-ordinates where element you are getting error, get both elements and display point co or dinates. That will definitively tell you. If my answer above is good can you upvote me. – Makjb lh Feb 25 '16 at 20:27
  • @Makjblh I agree that such UI behaviour should be considered a defect, depending on what is considered the minimum resolution for the site. And yes, focusing on an element before doing anything with it is good practice. Selenium doesn't always scroll to an element, depending on what the action is. – aneroid Feb 25 '16 at 20:31
  • @aneroid seems the user above is not worried about resolution nor he is doing usability testing as far as I understand. He seems to be doing functional testing. Similarly, his dilemma comes out of maximizing the browser, in that case first he needs to run both modes and observe. I dont think any configuration such as resolution is being tested particularly. – Makjb lh Feb 26 '16 at 02:26
0

In my current Capybara project, I often get "Element not clickable" errors. The reason for this kind of error is that web page doesn't loads well enough to locate the element. As you have mentioned that the tests previously passed and you haven't edited that part of the code base, So you can do the following things to get pass through this:

  1. Put some more wait time before locating "add_task_sidebar_link". So that the page loads fully.
  2. Still if the test fails even after adding wait, then add some method to take screenshot before clicking the element. This will help debugging the test.
Sarabjit Singh
  • 786
  • 4
  • 4
0

Since recently the Firefox driver checks that the element at the clicking position is the element supposed to be clicked. This error means that when the driver tries to click the element, an other one is on top of it. Unfortunatelly, the driver doesn't automatically scroll the containers and raises this error instead.

On way to handle this issue is to scroll the element in the center of the screen and try to click again. Monkey patch for the click method :

JS_SCROLL_ELEMENT_CENTER_WINDOW = """\
  var element = arguments[0];
  element.scrollIntoView(true);
  var y = (window.innerHeight - element.offsetHeight) / 2;
  if (y > 0) {
    for (var e=element; e; e=e.parentElement) {
      if (e.scrollTop) {
        var yy = Math.min(e.scrollTop, y);
        e.scrollTop -= yy;
        if ((y -= yy) < 1)
          return;
      }
    }
    window.scrollBy(0, -y);
  }
  """

def click(self):
  try:
    self._execute(Command.CLICK_ELEMENT)
  except:
    self._parent.execute(Command.EXECUTE_SCRIPT, \
      {'script': JS_SCROLL_ELEMENT_CENTER_WINDOW, 'args': [self]})
    self._execute(Command.CLICK_ELEMENT)

from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.remote.command import Command
WebElement.click = click
Florent B.
  • 41,537
  • 7
  • 86
  • 101
0

I had the same issue: due to a pop-up window, the element I had to click on would drift off the screen and become unclickable.

Scrolling the page to the element worked.

In Python:

elem = driver.find_element_by_tag_name("html")
elem.send_keys(Keys.END)
bfontaine
  • 18,169
  • 13
  • 73
  • 107
rp2017
  • 9
  • 2