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>


2
  • Update the question with the text based relevant HTML expanding the <i> element. Commented Dec 22, 2020 at 13:50
  • I have updated the question Commented Dec 22, 2020 at 14:07

2 Answers 2

2

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']"))))
Sign up to request clarification or add additional context in comments.

7 Comments

the button is still not responding.
@SahilGogia Are you able to locate the button with the xpath through devTools/console?
yes I can.. Using inspect element and I copy the xpath
@SahilGogia Checkout the updated answer and let me know the status.
selenium.common.exceptions.JavascriptException: Message: A JavaScript exception occured: null is not an object (evaluating 'arguments[0].click') is generated
|
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 

7 Comments

1st answer does nothing. 2nd answer causes exception selenium.common.exceptions.MoveTargetOutOfBoundsException:
Is the button visible on the screen ?
yes the button is visible... This is the page. zomato.com/mumbai/mcdonalds-dadar-west/order
Visible inside the view port ? Means you don't have to scroll to that button ?
I have to scroll to the button.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.