sgonline
sgonline

Reputation: 9

selenium button click python

I can't click a button containing JavaScript:

<div style="width:100%; float:left;"><div class="btn btn-search" onclick="javascript: search(); " style="cursor:pointer;width:100%" align="center">Ara</div></div>

I found the element, but the code below doesn't click:

browser.find_element_by_xpath('//div[@class="btn btn-search"]').click()

or

browser.find_element_by_xpath('//div[@onclick="javascript:"]').click()

This message is returned:

Message: unknown error: Element is not clickable at point (1153, 417)

Upvotes: 1

Views: 122

Answers (1)

Sudar
Sudar

Reputation: 101

From the error it looks like.

  1. The element is loaded into the DOM, but the position is not fixed on the UI yet.
  2. There can be some other divs not getting loaded completely.

Possible solutions:

Use WebdriverWait with click()

from selenium.webdriver.support.ui import WebDriverWait

wait = WebDriverWait(browser, 30)
element = wait.until(EC.visibility_of_element_located((By.XPATH, //div[@class="btn btn-search"]')))
element.click()

Use WebdriverWait with Javascript execution

from selenium.webdriver.support.ui import WebDriverWait

wait = WebDriverWait(browser, 30)
element = wait.until(EC.visibility_of_element_located((By.XPATH, '//div[@class="btn btn-search"]')))
browser.execute_script("arguments[0].click();", element)

Further Reference: https://www.seleniumeasy.com/selenium-tutorials/element-is-not-clickable-at-point-selenium-webdriver-exception

Also when you frame question please share,

  • Full error and the code (especially the line before you are trying to click).

  • Other details such as browser (from error looks like Chrome browser as it is indicating point location).

This will help the community understand your issue more clearly.

Upvotes: 1

Related Questions