Python Forum

Full Version: cant click button by script at page
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i try to click button, which, when clicked, should send the contract number and return data in the table

Quote:<button class="k-primary k-button k-button-icon" data-search="standard" id="standard-search2817" data-role="button" role="button" aria-disabled="false" tabindex="0" disabled=""><span class="fa fa-spin fa-spinner fa-3x"></span></button>
id is dynamic, it changes on the page, I can't take it

code:

        try:
            # Znajdź element za pomocą selektora CSS
            button_css_selector = 'button.k-primary.k-button.k-button-icon[data-search="standard"]'
        
            xpath_selector = '//button[@id and @data-search="standard"]'
            buttonXPath = driver.find_element(By.XPATH, xpath_selector)
            
            # Sprawdzanie, czy przycisk jest wyświetlany.
            if not buttonXPath.is_displayed():
                logging.warning("buttonXPath is not displayed. Attempting to display parent element.")
            
                # Znajdujemy rodzica przycisku.
                parent_element = buttonXPath.find_element(By.XPATH, './..')

                if not parent_element.is_displayed():
                    logging.warning("Parent element is hiding the button.")
                    driver.execute_script("arguments[0].style.display = 'block';", parent_element)


            # Use WebDriverWait to wait until the element is clickable
            button = driver.find_element(By.CSS_SELECTOR, button_css_selector)

            # Loguj informacje o zlokalizowanym elemencie
            logging.info(f"Element located: Tag Name: {button.tag_name}, Outer HTML: {button.get_attribute('outerHTML')}")
            
            
            # Make the button visible
            driver.execute_script("arguments[0].style.visibility = 'visible';", button)
            logging.info("Made the element visible using JS.")

            # Make the button active (removing the 'disabled' attribute)
            driver.execute_script("arguments[0].removeAttribute('disabled');", button)
            logging.info("Removed the 'disabled' attribute using JS.")
            
            size = button.size
            if size["height"] == 0 or size["width"] == 0:
                logging.warning("The element has no visible dimensions.")
            
            # Sprawdź widoczność elementu
            if button.is_displayed():
                logging.info("Element is visible.")
            else:
                logging.warning("Element is not visible.")
                
            # Sprawdź aktywność elementu
            if button.is_enabled():
                logging.info("Element is active and can be clicked.")
                # Przesuń się do elementu i kliknij
                ActionChains(driver).move_to_element(button).click().perform()
            else:
                logging.warning("Element is not active and cannot be clicked.")
            
            if button.is_displayed() and button.is_enabled():
                driver.execute_script("arguments[0].click();", button)
                logging.info("Clicked using JavaScript.")

            # Sprawdź, czy istnieją jakieś alerty
            try:
                alert = driver.switch_to.alert
                logging.warning(f"Alert present: {alert.text}")
            except NoAlertPresentException:
                logging.info("No alert present.")
            
            # Loguj położenie elementu
            location = button.location
            logging.info(f"Element location: X: {location['x']}, Y: {location['y']}")
            
        except TimeoutException as e:
            logging.error(f'Timeout exception occurred: {e}\n{traceback.format_exc()}')             
        except NoSuchElementException as e:
            logging.error(f'No such element exception occurred: {e}\n{traceback.format_exc()}')
        except ElementNotInteractableException as e:
            logging.error(f'Element not interactable exception occurred: {e}\n{traceback.format_exc()}')
            logging.info("Attempting to click using JavaScript.")
            driver.execute_script("arguments[0].click();", button)
            logging.info("Clicked using JavaScript.")
        except ElementClickInterceptedException as e:
            logging.error(f'Element click intercepted exception occurred: {e}\n{traceback.format_exc()}')
        except StaleElementReferenceException as e:
            logging.error(f'Stale element reference exception occurred: {e}\n{traceback.format_exc()}')
        except WebDriverException as e:
            logging.error(f'WebDriver exception occurred: {e}\n{traceback.format_exc()}')
        except Exception as e:
            logging.error(f'An unexpected error occurred: {e}\n{traceback.format_exc()}')
logs:
Quote: > 2023-10-04 14:11:20,188 - WARNING - buttonXPath is not displayed. Attempting to display parent element.

> 2023-10-04 14:11:20,223 - WARNING - Parent element is hiding the button.

> 2023-10-04 14:11:20,288 - INFO - Element located: Tag Name: button, Outer HTML: <button class="k-primary k-button k-button-icon" data-search="standard" id="standard-search2817" data-role="button" role="button" aria-disabled="false" tabindex="0" disabled=""><span class="fa fa-spin fa-spinner fa-3x"></span></button>

> 2023-10-04 14:11:20,299 - INFO - Made the element visible using JS.

> 2023-10-04 14:11:20,310 - INFO - Removed the 'disabled' attribute using JS.

> 2023-10-04 14:11:20,328 - WARNING - The element has no visible dimensions.

> 2023-10-04 14:11:20,344 - WARNING - Element is not visible.

> 2023-10-04 14:11:20,357 - INFO - Element is active and can be clicked.

> 2023-10-04 14:11:20,388 - ERROR - Element not interactable exception occurred: Message: element not interactable: [object HTMLButtonElement] has no size and location

(Session info: chrome=117.0.5938.132)
Stacktrace:
(...)

Traceback (most recent call last):
(...)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable: [object HTMLButtonElement] has no size and location
(Session info: chrome=117.0.5938.132)
Stacktrace:

(...)

> 2023-10-04 14:11:20,388 - INFO - Attempting to click using JavaScript.

> 2023-10-04 14:11:20,406 - INFO - Clicked using JavaScript.

> 2023-10-04 14:11:20,406 - INFO - Closing the browser in 15 seconds...
Hi @michael1834,

With which Python module are you interacting with the html DOM ?

Thanks.