from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import WebDriverWait def click_fetch_button(driver): wait = WebDriverWait(driver, 20) button = wait.until( EC.element_to_be_clickable( ( By.XPATH, "//*[self::button or self::a or self::input]" "[normalize-space()='Fetch' or contains(normalize-space(),'Fetch') or @value='Fetch']" "|//*[contains(normalize-space(),'Fetch')]/ancestor::*[self::button or self::a][1]" ) ) ) try: button.click() except Exception: driver.execute_script("arguments[0].click();", button) def paste_matrix_into_bidding_grid(driver, tsv_text, expected_first_value): matrix = [row.split("\t") for row in tsv_text.splitlines()] wait = WebDriverWait(driver, 30) wait.until( lambda _driver: _populate_handsontable(_driver, matrix) is not False ) wait.until( lambda _driver: _top_left_grid_value(_driver).strip() == str(expected_first_value).strip() ) def click_submit_button(driver): wait = WebDriverWait(driver, 20) button = wait.until( EC.element_to_be_clickable( (By.XPATH, "//button[normalize-space()='Submit']") ) ) try: button.click() except Exception: driver.execute_script("arguments[0].click();", button) def _top_left_grid_value(driver): cell = driver.find_element( By.XPATH, "(//table[contains(@class,'htCore')]//tbody/tr[1]/td[@role='gridcell'])[1]" ) return cell.text def _populate_handsontable(driver, matrix): return driver.execute_script( """ const matrix = arguments[0]; const instances = (window.Handsontable && window.Handsontable.instances) || []; for (const instance of instances) { if (!instance || !instance.rootElement) { continue; } const root = instance.rootElement; if (!root.isConnected || root.offsetParent === null) { continue; } const hasGridTable = !!root.querySelector("table.htCore tbody td[role='gridcell']"); const isInsideSelect2 = !!root.closest(".select2-drop, .select2-container, .select2-results"); const isInsidePopup = !!root.closest(".modal, .popover, .tooltip"); if (!hasGridTable || isInsideSelect2 || isInsidePopup) { continue; } try { instance.selectCell(0, 0); instance.populateFromArray(0, 0, matrix, null, null, "paste"); return instance.getDataAtCell(0, 0); } catch (error) { // Try the next visible instance. } } return false; """, matrix, )