0

i am running the below code to webscrape a site in python using selenium:

    def click_and_wait_for_additional_info(elem):
        # Click on the store element to navigate to its individual page
        elem.click()
        time.sleep(2)
        
        try:
            features = driver.find_element(By.XPATH,  '//*[@id="QA0Szd"]/div/div/div[1]/div[3]/div/div[1]/div/div/div[2]/div[6]/button/div[2]/div/div').text
        except:
            features = "None"
            
       
        return features
   

everytime i run this code on large number of observations, it gives error ElementClickInterceptedException

Any suggestions how this can be handled?

i tried below: a)driver.implicit wait b) below code:

def click_and_wait_for_additional_info(elem):
        try:
        # Wait for the element to be clickable
            clickable_elem = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//div[@class="Nv2PK tH5CWc THOPZb "]')))
            
            # Click on the store element to navigate to its individual page
            clickable_elem.click()
            time.sleep(2)
            
            try:
                features = driver.find_element(By.XPATH,  '//*[@id="QA0Szd"]/div/div/div[1]/div[3]/div/div[1]/div/div/div[2]/div[6]/button/div[2]/div/div').text
            except:
                features = "None"
                
           
            return category

        except ElementClickInterceptedException:
            print("Element click intercepted. Retrying...")
            # Retry clicking on the element
            try:
                clickable_elem = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//div[@class="Nv2PK tH5CWc THOPZb "]')))
                clickable_elem.click()
                time.sleep(2)
                # Your code to extract additional info goes here...
            except Exception as e:
                print("Exception occurred:", e)
                return None
    
        except Exception as e:
            print("Exception occurred:", e)
            return None

however in bot, while the error gets resolved, the data is: a) shiftef b) getting repeated

1
  • can you add link and and add on which button you want to click.? Commented Feb 26, 2024 at 16:47

2 Answers 2

0

It is most likely the locator issue.

The ElementClickInterceptedException exception is usually thrown when an attempt to click on an element on a web page is blocked by another element. This may happen when another element is overlapping or fully/partly positioned in front of the DOM element that you are trying to click.

I suggest you review your locator. Don't use the absolute paths, they are really unreliable and unstable. Use relative xpaths instead. See this article to understand the difference.

Also, don't rely on the @class attribute a lot. Especially if there are a lot of 'words'. Class values order as well as values themselves can be easily changed by developers really often.

Another point is the values. Ids like 'QA0Szd' or classes 'Nv2PK' are typically created automatically by the frontend frameworks. Usually, they are dynamic, so it is better to not use such attributes unless you trust the page source.

Basically, a good locator + explicit wait (EC.element_to_be_clickable) should do a trick for your case.
But in case it won't help, try clicking with actions.

Sign up to request clarification or add additional context in comments.

1 Comment

post making the changes, the data is getting repeated for first 2 merchants
0

I've run into this scenario several times and found it falls into a few different cases:

  1. The element I'm trying to click is permanently blocked by another element. In this case, I look at the HTML of the element provided in the intercepted exception message and find that element. Sometimes it's a header, footer, popup, etc. In that case, you either need to dismiss whatever's blocking the element or scroll the page.

  2. The element is temporarily blocked by another element. Maybe it's a progress bar or some popup that closes after a few seconds. In that case, I've created my own click() method to handle cases like this. Below is the method I use in my framework to click elements provided a locator. It uses WebDriverWait to wait for the element to be clickable before attempting a click. If it clicks and throws an intercepted or stale element exception, it tries again up to the timeout provided.

    def click(self, locator, time_out_in_seconds = 10):
        expire = datetime.now() + timedelta(seconds=time_out_in_seconds)
        while datetime.now() < expire:
            try:
                WebDriverWait(self.driver, time_out_in_seconds).until(EC.element_to_be_clickable(locator)).click()
                return
            except (ElementClickInterceptedException, StaleElementReferenceException):
                # do nothing, loop again
                pass
    
        raise Exception(f"Not able to click element <{locator}> within {time_out_in_seconds}s.")
    
  3. Something about the HTML of the page is causing elements to overlap. Usually it's a label overlapping some other element. In those cases, I try to find another locator that isn't overlapped.

  4. When all else fails, you can just use JS to click the blocked element.

    element = driver.find_element(By.ID, "myid")
    driver.execute_script("arguments[0].click();", element)
    

Comments

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.