[IMP] script selenium lib get elements and multiple click

This commit is contained in:
Mathieu Benoit 2026-05-30 01:49:17 -04:00
parent 89dc8e001f
commit 612c1e455a

View file

@ -482,6 +482,20 @@ class SeleniumLib(object):
button.click() button.click()
return button return button
def get_elements(
self, by: str = By.ID, value: str = None, timeout=defaut_timeout
):
wait = WebDriverWait(self.driver, timeout)
elements = wait.until(
EC.visibility_of_any_elements_located(
(
by,
value,
)
)
)
return elements
def get_element( def get_element(
self, self,
by: str = By.ID, by: str = By.ID,
@ -686,21 +700,39 @@ class SeleniumLib(object):
with_index: bool = False, with_index: bool = False,
is_visible=True, is_visible=True,
index_of_list: int = 0, index_of_list: int = 0,
support_multiple_click: bool = False,
): ):
# Exemple Detect button by text value
# Replace CLASS_NAME
# By.CSS_SELECTOR, "input.CLASS_NAME[value='Fermer']"
# ele = self.driver.find_element(by, value) # ele = self.driver.find_element(by, value)
if element: if element:
ele = element ele = element
else: else:
ele = self.get_element(by, value, timeout, is_visible=is_visible) if support_multiple_click:
ele = self.get_elements(by, value, timeout)
else:
ele = self.get_element(
by, value, timeout, is_visible=is_visible
)
if not no_scroll: if not no_scroll:
viewport_ele = None viewport_ele = None
if viewport_ele_value: if viewport_ele_value:
viewport_ele = self.get_element( viewport_ele = self.get_element(
viewport_ele_by, viewport_ele_value, timeout viewport_ele_by, viewport_ele_value, timeout
) )
self.scrollto_element(ele, viewport_ele=viewport_ele) if support_multiple_click:
# ActionChains(self.driver).move_to_element(ele).click().perform() for one_ele in ele:
ActionChains(self.driver).move_to_element(ele).perform() self.scrollto_element(one_ele, viewport_ele=viewport_ele)
# ActionChains(self.driver).move_to_element(one_ele).click().perform()
ActionChains(self.driver).move_to_element(
one_ele
).perform()
else:
self.scrollto_element(ele, viewport_ele=viewport_ele)
# ActionChains(self.driver).move_to_element(ele).click().perform()
ActionChains(self.driver).move_to_element(ele).perform()
time.sleep(self.config.selenium_default_delay) time.sleep(self.config.selenium_default_delay)
wait = WebDriverWait(self.driver, timeout) wait = WebDriverWait(self.driver, timeout)
if element: if element:
@ -717,26 +749,59 @@ class SeleniumLib(object):
) )
button = buttons[index_of_list] button = buttons[index_of_list]
else: else:
button = wait.until( if support_multiple_click:
EC.element_to_be_clickable( button = wait.until(
( EC.visibility_of_all_elements_located(
by, (
value, by,
value,
)
) )
) )
) else:
button = wait.until(
EC.element_to_be_clickable(
(
by,
value,
)
)
)
try: try:
button.click() if support_multiple_click:
for btn in button:
try:
btn.click()
# This button work!
return btn
except Exception as e:
pass
else:
button.click()
except ElementClickInterceptedException as e: except ElementClickInterceptedException as e:
try: try:
print(e) print(e)
self.driver.execute_script( if support_multiple_click:
"arguments[0].scrollIntoView(true);", button for btn in button:
) self.driver.execute_script(
button.click() "arguments[0].scrollIntoView(true);", btn
)
btn.click()
else:
self.driver.execute_script(
"arguments[0].scrollIntoView(true);", button
)
button.click()
except ElementClickInterceptedException as e: except ElementClickInterceptedException as e:
print(e) print(e)
self.driver.execute_script("arguments[0].click();", button) if support_multiple_click:
for btn in button:
self.driver.execute_script(
"arguments[0].click();", btn
)
else:
self.driver.execute_script("arguments[0].click();", button)
return button return button
def click_canvas_form( def click_canvas_form(