from random import uniform from selenium.common import TimeoutException from selenium.webdriver import ActionChains, Keys from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions from selenium.webdriver.support.wait import WebDriverWait from modules.Tools.logger import debug from modules.Tools.tools import * def set_language(ldriver): ldriver.get("chrome://settings/languages") action = ActionChains(ldriver) action.reset_actions() # select language x_coord = 1200 y_coord = 150 action.move_by_offset(x_coord, y_coord).click().perform() sleep(0.5) # scroll down action.reset_actions() elm = ldriver.find_element(By.XPATH, "/html/body") ActionChains(ldriver) \ .send_keys("french") \ .pause(0.5) \ .send_keys(Keys.TAB + Keys.TAB + Keys.ENTER + Keys.TAB + Keys.TAB + Keys.ENTER) \ .perform() x_coord = 1163 y_coord = 717 action.move_by_offset(x_coord, y_coord).click().perform() # put to the top sleep(0.5) action.reset_actions() x_coord = 1257 y_coord = 328 action.move_by_offset(x_coord, y_coord).click().perform() action.click().perform() # Deal with RGPD popup as well as some random popup like 'are you satisfied' one def rgpd_popup(config) -> None: for i in ["bnp_btn_accept", "bnp_hfly_cta2", "bnp_hfly_close"]: try: config.WebDriver.driver.find_element(By.ID, i).click() except Exception as err: debug(err) """ send_keys_wait([selenium element:element, str:keys]) send the different keys to the field element, with a random time between each press to simulate human action. keys can be an string, but also selenium keys """ def send_keys_wait(element, keys: str) -> None: for i in keys: element.send_keys(i) sleep(uniform(0.1, 0.3)) def send_wait_and_confirm(element, keys: str) -> None: send_keys_wait(element, keys) element.send_keys(Keys.ENTER) # Wait for the presence of the element identifier or [timeout]s def wait_until_visible(search_by: str, identifier: str, timeout: int = 20, browser=None) -> bool: try: WebDriverWait(browser, timeout).until( expected_conditions.visibility_of_element_located((search_by, identifier)), "element not found") return True except TimeoutException as e: error(f"element {identifier} not found after {timeout}s") return False