Python Forum
cant click button by script at page
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
cant click button by script at page
#1
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...
Reply
#2
Hi @michael1834,

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

Thanks.
[Image: NfRQr9R.jpg]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help for script access via webdriver to an open web page in Firefox Clixmaster 1 1,272 Apr-20-2023, 05:27 PM
Last Post: farshid
  Click on a button on web page using Selenium Pavel_47 7 4,725 Jan-05-2023, 04:20 AM
Last Post: ellapurnellrt
  button click error rafarangel 2 3,136 Feb-11-2021, 08:19 PM
Last Post: buran
  Unable to click element in web page Kalpana 0 1,877 Jun-25-2020, 05:20 AM
Last Post: Kalpana
  Log In Button Won't Click - Python Selenium Webdriver samlee916 2 3,841 Jun-07-2020, 04:42 PM
Last Post: samlee916
  How to click facebook message button JanelleGuthrie 2 2,420 May-14-2020, 06:02 PM
Last Post: Larz60+
  use Xpath in Python :: libxml2 for a page-to-page skip-setting apollo 2 3,646 Mar-19-2020, 06:13 PM
Last Post: apollo
  Contact form button click action Man_from_India 1 2,803 Feb-01-2020, 06:21 PM
Last Post: snippsat
  HOWTO? Login DSL Modem with Python Requests: need Click "Apply" Button Webtest 4 8,509 Aug-20-2019, 04:03 PM
Last Post: johnmina
  Selenium click on popup button??? GuJu 7 7,929 Jul-20-2019, 09:21 AM
Last Post: Nizam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020