Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to click the checkbox via Selenium in Python

Why am I unable to click the following checkbox on the page https://realty.yandex.ru/add via Selenium in Python?

enter image description here

import traceback

import selenium.webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import selenium.webdriver.support
import selenium.webdriver.support.ui


explicit_wait_timeout_secs = 10


def wait_for_element_presence(driver, find_type, web_element):
    return selenium.webdriver.support.ui.WebDriverWait(driver, explicit_wait_timeout_secs).until(EC.presence_of_element_located((find_type, web_element)))


def wait_for_element_clickable(driver, find_type, web_element):
    return selenium.webdriver.support.ui.WebDriverWait(driver, explicit_wait_timeout_secs).until(EC.element_to_be_clickable((find_type, web_element)))


try:
    driver = selenium.webdriver.Chrome()

    driver.get('https://realty.yandex.ru/add/')

    # element = wait_for_element_clickable(driver, By.NAME, 'lift')  # TimeoutException
    element = wait_for_element_presence(driver, By.NAME, 'lift')  # WebDriverException: Message: unknown error: Element is not clickable at point (203, 899). Other element would receive the click: <span class="checkbox__box">...</span>
    element.click()
except Exception:
    print('ERROR: \n' + traceback.format_exc())

try:
    driver.quit()
except Exception:
    pass

If I'm trying to wait for the "clickability" of this element, it gives me TimeoutException error. If I'm trying to wait for the presence of the element, it gives me "element is not clickable" error.

However, I can click this checkbox via Javascript:

driver.execute_script("document.getElementsByName('lift')[0].click();")

Also it works in Firefox btw.

Why? What am I doing wrong? How can I fix it?

Thanks in advance.

like image 286
FrozenHeart Avatar asked Dec 30 '14 14:12

FrozenHeart


People also ask

Why click is not working in Selenium Python?

We can list the most common reasons for click problems as being one of the following: Wrong web element locations. The existence of a web element that obscures the web element that we want to click. The Selenium WebDriver works much faster than the response of the application.

How do you click a checkbox in python Selenium?

We can check a checkbox in a page in Selenium with the help of click() method. First of all we need to uniquely identify the checkbox with the help of any of the locators like css, xpath, id, class and so on. Next we have to use findElement() method to locate the element and finally perform the clicking action.

Why can't I click on a button in Selenium?

Selenium clicks fail when an element is not attached to the current page. Re-find the element if the previously found element has been detached.

How can you select a checkbox in Selenium WebDriver?

We can select a checkbox either by using the click() method on the input node or on the label node that represents the checkbox. Selenium also offers validation methods like isSelected, isEnabled, and isDisplayed.


1 Answers

You need to click on the span tag that is a parent of a parent of the input tag with name="lift":

element = driver.find_element_by_xpath('//span[span/input[@name="lift"]]')
element.click()

Works for me in both Chrome and Firefox:

enter image description here

To be safe, you can also scroll to an element before clicking:

def scroll_element_into_view(driver, element):
    """Scroll element into view"""
    y = element.location['y']
    driver.execute_script('window.scrollTo(0, {0})'.format(y))

element = driver.find_element_by_xpath('//span[span/input[@name="lift"]]')
scroll_element_into_view(driver, element)
element.click()
like image 196
alecxe Avatar answered Sep 22 '22 19:09

alecxe