Home > Software design >  Python Selenium - clicking Send button 'not clickable
Python Selenium - clicking Send button 'not clickable

Time:07-07

I have a python script where I need to click a button. My function is as follows:

def inviteuser():
    invitebutton.click()
    time.sleep(2.5)
    addressbox = driver.find_element_by_xpath('/html/body/div[9]/div/div/div[2]/div/div[1]/div/div/div/div/div[3]/div/div/div[1]')
    time.sleep(2.5)
    addressbox.send_keys(email)
    time.sleep(2.5)
    sendbutton = driver.find_element_by_xpath('/html/body/div[8]/div/div/div[3]/div[2]')
    sendbutton.click()

When running the script at the button clicking part, I get this message: selenium.common.exceptions.ElementClickInterceptedException: Message: Element <div > is not clickable at point (834,677) because another element <div > obscures it

I tried searching for that div, but the search in the browser could not find it.

I also tried driver.find_element_by_css_selector('.c-button .c-button--primary .c-button--medium').click()

HTML code of the items

<div >
<button  data-qa="invite-to-workspace-modal-invite-form-send-button" type="button" aria-disabled="true">
"Send"
::after
</button>
</div>

If it helps at all, this is for the invite people box in slack admin portal

CodePudding user response:

Try the execute script method which can have a better chance when getting that error. Also you could use implicitly wait instead of time waits to be more effecient.

driver.implicitly_wait(5)

addressbox = driver.find_element_by_xpath('/html/body/div[9]/div/div/div[2]/div/div[1]/div/div/div/div/div[3]/div/div/div[1]')

driver.execute_script("arguments[0].click();", addressbox)

CodePudding user response:

Generally <div> elements aren't clickable. Presumably you need to crosscheck if the following element is your desired element:

<div >

Where as, the element from your second attempt:

<div >

looks as a perfect clickable candidate.


Solution

The desired element is a React element, so to click() on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".c-button.c-button--primary.c-button--medium"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@class='c-button c-button--primary c-button--medium']"))).click()
    
  • Note: You have to add the following imports :

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