问题
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>
回答1:
Your Answer lies within your question.
- 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. - 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)
回答2:
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!
回答3:
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)
回答4:
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)
回答5:
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)
回答6:
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:
- Put some more wait time before locating "add_task_sidebar_link". So that the page loads fully.
- 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.
回答7:
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
来源:https://stackoverflow.com/questions/35636155/selenium-element-not-clickable-other-element-would-receive-click