0

I chose the xpath from here

This is the button I am trying to click

add =driver.find_element_by_xpath('//*[@id="root"]/div/main/div/section[4]/section/section[2]/section[2]/div[2]/div[1]/div/div/div[2]/div/div[2]/div')
add.click()
print("hey") 

This is the code, add.click() does not generate any error and hey is printed but the button is not clicked..

Initially the add button is not in the viewport but when the code executes it automatically comes in the view port but nothing happens.

I have tried doing everything like scroll to element, and brought the element in the viewpoet but still nothing happens..

This is from where I got the xpath


<div class="sc-1usozeh-8 kTTqJP">

  <span class="sc-1usozeh-6 fTsfFl">Add</span>

  <i class="rbbb40-1 MxLSp sc-1usozeh-4 TZpZK" size="14" color="#ED5A6B">.
 
    <svg xmlns="http://www.w3.org/2000/svg" fill="#ED5A6B" width="14"    
     height="14" viewBox="0 0 20 20" aria-labelledby="icon-svg-title-  
     icon-svg-desc-" role="img" class="rbbb40-0 hoSSCx">
 
      <title>plus</title>

      <path d="M15.5 9.42h-4.5v-4.5c0-0.56-0.44-1-1-1s-1 0.44-1 1v4.5h-
      4.5c-0.56 0-1 0.44-1 1s0.44 1 1 1h4.5v4.5c0 0.54 0.44 1 1 1s1-0.46 
      1-1v-4.5h4.5c0.56 0 1-0.46 1-1s-0.44-1-1-1z"></path>

   </svg>
 </i>
</div>


Rocket
  • 123
  • 8

2 Answers2

1

The element is a dynamic 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 XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Add']"))).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
    

Update

As a last resort you can use execute_script() as follows:

driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Add']"))))
Nic Laforge
  • 1,776
  • 1
  • 8
  • 14
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Add']/.."))).click()

If above doesn't work try

button = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Add']")))

driver.execute_script("arguments[0].scrollIntoView();", button)


action = ActionChains(driver)

action.move_to_element(button).click().perform()

You need to import action class:

from selenium.webdriver.common.action_chains import ActionChains 
PDHide
  • 18,113
  • 2
  • 31
  • 46